C++ 中的集装箱船或嵌套是什么?
我的理解是: Containership:一个类包含另一个类对象作为成员数据。
请举例说明。
谢谢。
what i understood is:
Containership: a class contains another class objects as member data.
please explain with an example.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
类嵌套只是在另一个类中定义一个类,如下所示:
然后您可以使用作用域运算符访问嵌套类,就像使用命名空间一样:
现在,当一个类包含另一个类的对象,它是一个聚合:
如果它是公共的,那么您可以像这样访问该成员:
如果它不可访问,那么您可以通过功能。无论如何,在类函数内部,您可以直接访问成员:
Class nesting is simply to have a class defined in another class, like that :
Then you can access a nested class using the scope operator, like you would do with namespaces :
Now when a class contain another class's object, it's an aggregation :
Then you can access the member like that if it's public :
If it's not accessible, then you can provide it via a function. Anyway, inside the class functions, you can directly access the member :
Containership仅仅意味着一个对象可以在另一个对象内部访问。
例如:
您可以像这样访问 foo :
类嵌套是不同的。这意味着您正在从另一个类中声明一个类:
Containership simply means an object can be accessed inside another.
For instance :
You can access foo like this :
Class nesting is different. It means you are declaring a class from inside another :
与继承类似,
A 类
的对象嵌入在B 类
的对象中:如果您实例化
B b
,您会得到一些东西在您的记忆中,就像这样:与指针属性相反,嵌套对象实际上位于外部对象内部。此外,您不必担心创建/删除
A
,因为创建/删除B
时会自动完成。Similar to inheritance, an object of
class A
is embedded in the object ofclass B
:If you would instantiate
B b
, you would get something like this on your memory:Contrary to a pointer attribute, a nested object actually sits inside the outer object. Also you do not have to worry about creating/deleting
A
as it is automatically done whenB
is created/deleted.