【发布时间】:2011-02-02 08:19:13
【问题描述】:
谁能给我一个例子,说明如何在 C++ 中的 class 中定义一种新类型的 struct。
谢谢。
【问题讨论】:
-
另请注意,最受好评的答案的相同技术可用于在类中定义类,在结构中定义结构,在结构中定义类。 class 和 struct 仅在其成员的默认可见性(分别为私有和公共)方面有所不同。
-
...以及它们的默认继承类型(分别为私有和公有)。
谁能给我一个例子,说明如何在 C++ 中的 class 中定义一种新类型的 struct。
谢谢。
【问题讨论】:
类似这样的:
class Class {
// visibility will default to private unless you specify it
struct Struct {
//specify members here;
};
};
【讨论】:
可能在某些头文件中声明类和嵌套结构
class C {
// struct will be private without `public:` keyword
struct S {
// members will be public without `private:` keyword
int sa;
void func();
};
void func(S s);
};
如果你想分离实现/定义,可能在一些 CPP 文件中
void C::func(S s) {
// implementation here
}
void C::S::func() { // <= note that you need the `full path` to the function
// implementation here
}
如果你想内联实现,其他答案就可以了。
【讨论】:
这里的其他答案已经演示了如何在类中定义结构。还有另一种方法可以做到这一点,那就是在类内部声明结构,但在外部定义它。这可能很有用,例如,如果结构相当复杂并且可能以一种可以从其他地方详细描述中受益的方式独立使用。
其语法如下:
class Container {
...
struct Inner; // Declare, but not define, the struct.
...
};
struct Container::Inner {
/* Define the struct here. */
};
您通常会在定义嵌套类而不是结构的上下文中看到这一点(一个常见的例子是为集合类定义迭代器类型),但我认为为了完整性,值得在这里炫耀一下。
【讨论】:
类似:
class Tree {
struct node {
int data;
node *llink;
node *rlink;
};
.....
.....
.....
};
【讨论】:
是的,你可以。在 C++ 中,类和结构有点相似。我们不仅可以在一个类中定义结构,还可以在一个类中定义一个类。它被称为内部类。
作为一个例子,我正在添加一个简单的 Trie 类。
class Trie {
private:
struct node{
node* alp[26];
bool isend;
};
node* root;
node* createNode(){
node* newnode=new node();
for(int i=0; i<26; i++){
newnode->alp[i]=nullptr;
}
newnode->isend=false;
return newnode;
}
public:
/** Initialize your data structure here. */
Trie() {
root=createNode();
}
/** Inserts a word into the trie. */
void insert(string word) {
node* head=root;
for(int i=0; i<word.length(); i++){
if(head->alp[int(word[i]-'a')]==nullptr){
node* newnode=createNode();
head->alp[int(word[i]-'a')]=newnode;
}
head=head->alp[int(word[i]-'a')];
}
head->isend=true;
}
/** Returns if the word is in the trie. */
bool search(string word) {
node* head=root;
for(int i=0; i<word.length(); i++){
if(head->alp[int(word[i]-'a')]==nullptr){
return false;
}
head=head->alp[int(word[i]-'a')];
}
if(head->isend){return true;}
return false;
}
/** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
node* head=root;
for(int i=0; i<prefix.length(); i++){
if(head->alp[int(prefix[i]-'a')]==nullptr){
return false;
}
head=head->alp[int(prefix[i]-'a')];
}
return true;
}
};
/**
* Your Trie object will be instantiated and called as such:
* Trie* obj = new Trie();
* obj->insert(word);
* bool param_2 = obj->search(word);
* bool param_3 = obj->startsWith(prefix);
*/
【讨论】:
#include<iostream>
using namespace std;
class A
{
public:
struct Assign
{
public:
int a=10;
float b=20.5;
private:
double c=30.0;
long int d=40;
};
struct Assign ALT;
};
class B: public A
{
public:
int x = 10;
private:
float y = 20.8;
};
int main()
{
B myobj;
A obj;
//cout<<myobj.a<<endl;
//cout<<myobj.b<<endl;
//cout<<obj.a<<endl;
//cout<<obj.b<<endl;
cout<<myobj.ALT.a<<endl;
return 0;
}
enter code here
【讨论】: