在 C++ 中的类中定义一个结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2543205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Define a struct inside a class in C++
提问by small_potato
Can someone give me an example about how to define a new type of structin a classin C++.
有人能给我一个关于如何在 C++的类中定义新类型结构的例子。
Thanks.
谢谢。
回答by sharptooth
Something like this:
像这样的东西:
class Class {
// visibility will default to private unless you specify it
struct Struct {
//specify members here;
};
};
回答by Afriza N. Arief
declare class & nested struct probably in some header file
可能在某些头文件中声明类和嵌套结构
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);
};
if you want to separate the implementation/definition, maybe in some CPP file
如果你想分离实现/定义,也许在一些 CPP 文件中
void C::func(S s) {
// implementation here
}
void C::S::func() { // <= note that you need the `full path` to the function
// implementation here
}
if you want to inline the implementation, other answers will do fine.
如果你想内联实现,其他答案就可以了。
回答by codaddict
Something like:
就像是:
class Tree {
struct node {
int data;
node *llink;
node *rlink;
};
.....
.....
.....
};
回答by templatetypedef
The other answers here have demonstrated how to define structs inside of classes. There's another way to do this, and that's to declarethe struct inside the class, but defineit outside. This can be useful, for example, if the struct is decently complex and likely to be used standalone in a way that would benefit from being described in detail somewhere else.
这里的其他答案演示了如何在类中定义结构。还有另一种方法可以做到这一点,那就是在类内部声明结构,但在外部定义它。这可能很有用,例如,如果结构体相当复杂并且可能以某种方式独立使用,从而受益于在其他地方的详细描述。
The syntax for this is as follows:
其语法如下:
class Container {
...
struct Inner; // Declare, but not define, the struct.
...
};
struct Container::Inner {
/* Define the struct here. */
};
You more commonly would see this in the context of defining nested classes rather than structs (a common example would be defining an iterator type for a collection class), but I thought for completeness it would be worth showing off here.
您更常见的是在定义嵌套类而不是结构的上下文中看到这一点(一个常见的例子是为集合类定义迭代器类型),但我认为为了完整性,值得在这里炫耀。
回答by AKASH KUMAR GUPTA
#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
回答by TechCat
Yes you can. In c++, class and struct are kind of similar. We can define not only structure inside a class, but also a class inside one. It is called inner class.
是的你可以。在 C++ 中,class 和 struct 有点相似。我们不仅可以在类中定义结构,还可以在类中定义类。它被称为内部类。
As an example I am adding a simple Trie class.
例如,我添加了一个简单的 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);
*/