I am looking at the implementation of an API that I am using.
我正在查看正在使用的API的实现。
I noticed that a struct is inheriting from a class and I paused to ponder on it...
我注意到一个结构体继承了一个类,我停下来思考它……
First, I didn't see in the C++ manual I studied with that a struct could inherit from another struct:
首先,在我学习的c++手册中,我没有看到一个struct可以从另一个struct继承:
struct A {};
struct B : public A {};
I guess that in such a case, struct B inherits from all the data in stuct A. Can we declare public/private members in a struct?
我想在这种情况下,struct B继承了stuct a中的所有数据,我们能否在struct中声明public/private成员?
But I noticed this:
但我注意到:
class A {};
struct B : public A {};
From my online C++ manual:
我的在线c++手册:
A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.
Is the above inheritance valid even if class A has some member functions? What happen to the functions when a struct inherit them? And what about the reverse: a class inheriting from a struct?
即使类A有一些成员函数,上面的继承是否有效?当结构继承函数时,函数会发生什么?那么反过来:从结构中继承的类呢?
Practically speaking, I have this:
实际上,我有:
struct user_messages {
std::list<std::string> messages;
};
And I used to iterate over it like this foreach message in user_messages.messages
.
我曾经在user_messages.messages中对它进行遍历。
If I want to add member functions to my struct, can I change its declaration and "promote" it to a class, add functions, and still iterate over my user_messages.messages as I did before?
如果我想向我的结构中添加成员函数,我可以更改它的声明并将其“提升”到一个类中,添加函数,并在user_messages上进行迭代吗?就像我之前做的那样?
Obviously, I am still a newbie and I am still unclear how structs and classes interact with each other, what's the practical difference between the two, and what the inheritance rules are...
显然,我还是个新手,我仍然不清楚结构体和类如何相互作用,两者之间的实际区别是什么,继承规则是什么……
9 个解决方案
#1
95
Yes, struct can inherit from class in C++.
是的,struct可以从c++中的类继承。
In C++, classes and struct are the same except for their default behaviour with regards to inheritance and access levels of members.
在c++中,类和结构是相同的,除了它们在成员的继承和访问级别方面的默认行为。
C++ class
- Default Inheritance = private
- 默认继承=私人
- Default Access Level for Member Variables and Functions = private
- 成员变量和函数的默认访问级别= private
C++ struct
- Default Inheritance = public
- 默认继承=公共
- Default Access Level for Member Variables and Functions = public
- 成员变量和函数的默认访问级别= public
#2
32
In C++
在c++中
struct A { /* some fields/methods ... */ };
is equivalent to:
等价于:
class A { public: /* some fields/methods ... */ };
And
和
class A { /* some fields/methods ... */ };
is equivalent to:
等价于:
struct A { private: /* some fields/methods ... */ };
That means that the members of a struct/class are by default public/private.
这意味着结构体/类的成员默认为public/private。
Using struct
also changes the default inheritance to public
, i.e.
使用struct还将默认继承更改为public,即。
struct A { }; // or: class A { };
class B : A { };
is equivalent to
相当于
struct A { }; // or: class A { };
struct B : private A { };
And the other way around, this
反过来,这个
struct A { }; // or: class A { };
struct B : A { };
is equivalent to:
等价于:
struct A { }; // or: class A { };
class B : public A { };
Summary: Yes, a struct
can inherit from a class. The difference between the class
and struct
keywords is just a change in the default private/public specifiers.
概要:是的,结构可以从类继承。类和struct关键字之间的区别仅仅是默认的私有/公共说明符的更改。
#3
7
The only difference between a struct and a class is the default access level for members (private for classes, public for structs). This means that a struct should be able to inherit from a class, and vice-versa.
结构体和类之间的唯一区别是成员的默认访问级别(类是私有的,结构体是公有的)。这意味着结构体应该能够从类继承,反之亦然。
However, there is usually a difference in how structs and classes are used that is not mandated by the standard. structs are often used for pure data, ( or objects without polymorphism depending on your projects preference) and classes are used for the other cases. I emphasise that this is just a stylistic difference and not required.
然而,在结构和类的使用上通常有不同,这不是标准规定的。结构通常用于纯数据(或不依赖于您的项目首选项的对象)和类用于其他情况。我强调这只是一种风格上的差异,并不是必须的。
#4
6
The main thing to understand is that structs come from C, whereas classes are C++. This means that although structs ARE first-class object-orientated citizens, they also have a legacy purpose, which is the reason for classes being separate and structs being default-access public. However, once this is done with, they're absolutely and totally identical and interchangable in every way.
需要理解的主要内容是结构体来自C,而类是c++。这意味着,尽管结构体是面向对象的一级公民,但它们也有遗留的目的,这就是类被分离和结构被默认访问公共的原因。然而,一旦完成,它们在各个方面都是绝对的、完全相同的、可互换的。
#5
5
A struct is the same thing as a class except that a class defaults its members to private while a struct defaults its members to public. As a result, yes, you can inherit between the two. See in C++, can I derive a class from a struct.
struct与类是一样的,只是一个类将其成员默认为private,而一个struct将其成员默认为public。因此,是的,你可以在两者之间继承。在c++中,我可以从结构中派生类吗?
#6
4
struct and class are pretty much interchangeable - just with different defaults in that classes default to private inheritance and members, structs to public. The class keyword (and not struct) must be used for eg. "template <class T>".
struct和class几乎是可以互换的——只是类中的不同默认值默认为私有继承,成员默认为公有结构。类关键字(而不是struct)必须用于eg。“模板 <类t> ”。
That said, many programmers use the two to give a slight suggestion to a programmer reading the code: by using a struct you're subtly suggesting a less encapsulating, OO design. A struct might be used internal to a library - where getting at the guts of it all is fair game, whereas classes are used on the boundary where API changes would inconvenience clients and better abstraction is useful. This very loose convention has grown out of the difference in default accessibility - lazy/efficient/concise (take your pick) programmers do what's easiest unless there's a benefit otherwise, and not typing access specifiers is nice when possible.
也就是说,许多程序员使用这两种方式向阅读代码的程序员提供一个小小的建议:通过使用结构体,您可以巧妙地建议一种不那么封装的OO设计。一个struct可能会被应用到一个库中——在这个库中获取所有的内容都是公平的游戏,而类则是在API更改会给客户带来不便的边界上使用,而更好的抽象是有用的。这种非常松散的约定源自于默认可访问性的差异——惰性/高效/简洁(随您选择)程序员做最简单的事情,除非有其他好处,否则不输入访问说明符是最好的。
#7
4
Yes a struct
can inherit from a class. struct
and class
differ only in the access-specifier assumed for the members and for a base classes (or structs) if not specified explicitly in C++ . For structs it's public
. For classes it's private
.
是的,结构可以从类继承。如果在c++中没有显式地指定,结构和类只在为成员假定的访问说明符以及为基类(或结构)假设的访问说明符中有所不同。结构体是公开的。类是私有的。
The sentence you quote from the manual is about the concept of a class in C++, as compared to the concept of a data structure in C. In C++ new keyword - class
was introduced to better reflect the change in the concept, but for compatibility with code in C, an old keyword struct
was left and it's meaning is as described above.
你引用的句子手册是关于c++的类的概念,相对于一个数据结构的概念C在c++中引入新的关键字——类,以更好地反映概念的变化,但对于兼容C代码,一个老字结构了,它的意思是如上所述。
#8
4
Yes. Struct can inherit from a class and vice versa. The accessibility rule is
是的。Struct可以从类继承,反之亦然。可访问性的规则是
$11.2/2- "In the absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class."
$11.2/2-“如果基类没有访问说明符,则在声明派生类为struct时假定为public,在声明类时假定为private。”
EDIT 2: So you can change your class as:. Note that it is a bad idea to have public data members usually.
编辑2:这样你就可以把你的类改成:。请注意,通常使用公共数据成员是一个坏主意。
class user_messages { // i also changed the name when OP was modified :)
public:
std::list<std::string> messages;
};
#9
1
A class will not publicly inherit from a struct. A struct will publicly inherit from a class or a struct.
类不会从结构体中公开继承。结构体将从类或结构体中公开继承。
class A
{
public:
int a;
};
struct B : A
{};
B b; b.a=5; //OK. a is accessible
B B;本科= 5;/ /好吧。一个访问
class A
{
public:
int a;
};
struct B : public A
{};
It means the same. B b; b.a=5; //OK. a is accessible
这意味着相同的。B B;本科= 5;/ /好吧。一个访问
struct A
{int a;};
class B : A
{};
B b; b.a=5; //NOT OK. a is NOT accessible
B B;本科= 5;/ /不可以。一个是无法访问
struct A
{int a;};
class B : public A
{};
B b; b.a=5; //OK. a is accessible
B B;本科= 5;/ /好吧。一个访问
Finally:
最后:
class A
{int a;};
class B : A
{};
B b; b.a=5; //NOT OK. a is NOT accessible
B B;本科= 5;/ /不可以。一个是无法访问
class A
{int a;};
class B : public A
{};
B b; b.a=5; //NOT OK. a is NOT accessible
B B;本科= 5;/ /不可以。一个是无法访问
#1
95
Yes, struct can inherit from class in C++.
是的,struct可以从c++中的类继承。
In C++, classes and struct are the same except for their default behaviour with regards to inheritance and access levels of members.
在c++中,类和结构是相同的,除了它们在成员的继承和访问级别方面的默认行为。
C++ class
- Default Inheritance = private
- 默认继承=私人
- Default Access Level for Member Variables and Functions = private
- 成员变量和函数的默认访问级别= private
C++ struct
- Default Inheritance = public
- 默认继承=公共
- Default Access Level for Member Variables and Functions = public
- 成员变量和函数的默认访问级别= public
#2
32
In C++
在c++中
struct A { /* some fields/methods ... */ };
is equivalent to:
等价于:
class A { public: /* some fields/methods ... */ };
And
和
class A { /* some fields/methods ... */ };
is equivalent to:
等价于:
struct A { private: /* some fields/methods ... */ };
That means that the members of a struct/class are by default public/private.
这意味着结构体/类的成员默认为public/private。
Using struct
also changes the default inheritance to public
, i.e.
使用struct还将默认继承更改为public,即。
struct A { }; // or: class A { };
class B : A { };
is equivalent to
相当于
struct A { }; // or: class A { };
struct B : private A { };
And the other way around, this
反过来,这个
struct A { }; // or: class A { };
struct B : A { };
is equivalent to:
等价于:
struct A { }; // or: class A { };
class B : public A { };
Summary: Yes, a struct
can inherit from a class. The difference between the class
and struct
keywords is just a change in the default private/public specifiers.
概要:是的,结构可以从类继承。类和struct关键字之间的区别仅仅是默认的私有/公共说明符的更改。
#3
7
The only difference between a struct and a class is the default access level for members (private for classes, public for structs). This means that a struct should be able to inherit from a class, and vice-versa.
结构体和类之间的唯一区别是成员的默认访问级别(类是私有的,结构体是公有的)。这意味着结构体应该能够从类继承,反之亦然。
However, there is usually a difference in how structs and classes are used that is not mandated by the standard. structs are often used for pure data, ( or objects without polymorphism depending on your projects preference) and classes are used for the other cases. I emphasise that this is just a stylistic difference and not required.
然而,在结构和类的使用上通常有不同,这不是标准规定的。结构通常用于纯数据(或不依赖于您的项目首选项的对象)和类用于其他情况。我强调这只是一种风格上的差异,并不是必须的。
#4
6
The main thing to understand is that structs come from C, whereas classes are C++. This means that although structs ARE first-class object-orientated citizens, they also have a legacy purpose, which is the reason for classes being separate and structs being default-access public. However, once this is done with, they're absolutely and totally identical and interchangable in every way.
需要理解的主要内容是结构体来自C,而类是c++。这意味着,尽管结构体是面向对象的一级公民,但它们也有遗留的目的,这就是类被分离和结构被默认访问公共的原因。然而,一旦完成,它们在各个方面都是绝对的、完全相同的、可互换的。
#5
5
A struct is the same thing as a class except that a class defaults its members to private while a struct defaults its members to public. As a result, yes, you can inherit between the two. See in C++, can I derive a class from a struct.
struct与类是一样的,只是一个类将其成员默认为private,而一个struct将其成员默认为public。因此,是的,你可以在两者之间继承。在c++中,我可以从结构中派生类吗?
#6
4
struct and class are pretty much interchangeable - just with different defaults in that classes default to private inheritance and members, structs to public. The class keyword (and not struct) must be used for eg. "template <class T>".
struct和class几乎是可以互换的——只是类中的不同默认值默认为私有继承,成员默认为公有结构。类关键字(而不是struct)必须用于eg。“模板 <类t> ”。
That said, many programmers use the two to give a slight suggestion to a programmer reading the code: by using a struct you're subtly suggesting a less encapsulating, OO design. A struct might be used internal to a library - where getting at the guts of it all is fair game, whereas classes are used on the boundary where API changes would inconvenience clients and better abstraction is useful. This very loose convention has grown out of the difference in default accessibility - lazy/efficient/concise (take your pick) programmers do what's easiest unless there's a benefit otherwise, and not typing access specifiers is nice when possible.
也就是说,许多程序员使用这两种方式向阅读代码的程序员提供一个小小的建议:通过使用结构体,您可以巧妙地建议一种不那么封装的OO设计。一个struct可能会被应用到一个库中——在这个库中获取所有的内容都是公平的游戏,而类则是在API更改会给客户带来不便的边界上使用,而更好的抽象是有用的。这种非常松散的约定源自于默认可访问性的差异——惰性/高效/简洁(随您选择)程序员做最简单的事情,除非有其他好处,否则不输入访问说明符是最好的。
#7
4
Yes a struct
can inherit from a class. struct
and class
differ only in the access-specifier assumed for the members and for a base classes (or structs) if not specified explicitly in C++ . For structs it's public
. For classes it's private
.
是的,结构可以从类继承。如果在c++中没有显式地指定,结构和类只在为成员假定的访问说明符以及为基类(或结构)假设的访问说明符中有所不同。结构体是公开的。类是私有的。
The sentence you quote from the manual is about the concept of a class in C++, as compared to the concept of a data structure in C. In C++ new keyword - class
was introduced to better reflect the change in the concept, but for compatibility with code in C, an old keyword struct
was left and it's meaning is as described above.
你引用的句子手册是关于c++的类的概念,相对于一个数据结构的概念C在c++中引入新的关键字——类,以更好地反映概念的变化,但对于兼容C代码,一个老字结构了,它的意思是如上所述。
#8
4
Yes. Struct can inherit from a class and vice versa. The accessibility rule is
是的。Struct可以从类继承,反之亦然。可访问性的规则是
$11.2/2- "In the absence of an access-specifier for a base class, public is assumed when the derived class is declared struct and private is assumed when the class is declared class."
$11.2/2-“如果基类没有访问说明符,则在声明派生类为struct时假定为public,在声明类时假定为private。”
EDIT 2: So you can change your class as:. Note that it is a bad idea to have public data members usually.
编辑2:这样你就可以把你的类改成:。请注意,通常使用公共数据成员是一个坏主意。
class user_messages { // i also changed the name when OP was modified :)
public:
std::list<std::string> messages;
};
#9
1
A class will not publicly inherit from a struct. A struct will publicly inherit from a class or a struct.
类不会从结构体中公开继承。结构体将从类或结构体中公开继承。
class A
{
public:
int a;
};
struct B : A
{};
B b; b.a=5; //OK. a is accessible
B B;本科= 5;/ /好吧。一个访问
class A
{
public:
int a;
};
struct B : public A
{};
It means the same. B b; b.a=5; //OK. a is accessible
这意味着相同的。B B;本科= 5;/ /好吧。一个访问
struct A
{int a;};
class B : A
{};
B b; b.a=5; //NOT OK. a is NOT accessible
B B;本科= 5;/ /不可以。一个是无法访问
struct A
{int a;};
class B : public A
{};
B b; b.a=5; //OK. a is accessible
B B;本科= 5;/ /好吧。一个访问
Finally:
最后:
class A
{int a;};
class B : A
{};
B b; b.a=5; //NOT OK. a is NOT accessible
B B;本科= 5;/ /不可以。一个是无法访问
class A
{int a;};
class B : public A
{};
B b; b.a=5; //NOT OK. a is NOT accessible
B B;本科= 5;/ /不可以。一个是无法访问