C++ 中的集装箱船或嵌套是什么?

发布于 2024-11-07 04:19:44 字数 73 浏览 0 评论 0原文

我的理解是: Containership:一个类包含另一个类对象作为成员数据。

请举例说明。

谢谢。

what i understood is:
Containership: a class contains another class objects as member data.

please explain with an example.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

翻了热茶 2024-11-14 04:19:44

类嵌套只是在另一个类中定义一个类,如下所示:

class A
{
public:
   class B 
   {
   public:
       class C{};
   };

};

然后您可以使用作用域运算符访问嵌套类,就像使用命名空间一样:

A a;
A::B b;
A::B::C c;

现在,当一个类包含另一个类的对象,它是一个聚合

class D
{
public:

   A myA;

   void do_something();

private:
   A::B myB;

};

如果它是公共的,那么您可以像这样访问该成员:

D d;
process( d.myA ); // access to myA

如果它不可访问,那么您可以通过功能。无论如何,在类函数内部,您可以直接访问成员:

void D::do_something()
{
    doit( myB );
    // or
    doit( this->myB );
}

Class nesting is simply to have a class defined in another class, like that :

class A
{
public:
   class B 
   {
   public:
       class C{};
   };

};

Then you can access a nested class using the scope operator, like you would do with namespaces :

A a;
A::B b;
A::B::C c;

Now when a class contain another class's object, it's an aggregation :

class D
{
public:

   A myA;

   void do_something();

private:
   A::B myB;

};

Then you can access the member like that if it's public :

D d;
process( d.myA ); // access to myA

If it's not accessible, then you can provide it via a function. Anyway, inside the class functions, you can directly access the member :

void D::do_something()
{
    doit( myB );
    // or
    doit( this->myB );
}
小苏打饼 2024-11-14 04:19:44

Containership仅仅意​​味着一个对象可以在另一个对象内部访问。

例如:

class Contained
{
    int foo;
};

class Container
{
    Contained bar;
};

您可以像这样访问 foo :

Container c;
c.bar.foo = 42;

类嵌套是不同的。这意味着您正在从另一个类中声明一个类:

class AngryMammoth
{
    class CrazyVulture
    {
        int legCount;
    };

    int numberOfPeopleKilledSoFar;
};

Containership simply means an object can be accessed inside another.

For instance :

class Contained
{
    int foo;
};

class Container
{
    Contained bar;
};

You can access foo like this :

Container c;
c.bar.foo = 42;

Class nesting is different. It means you are declaring a class from inside another :

class AngryMammoth
{
    class CrazyVulture
    {
        int legCount;
    };

    int numberOfPeopleKilledSoFar;
};
为你拒绝所有暧昧 2024-11-14 04:19:44
class In {};
class Out {
  In object;
};
class In {};
class Out {
  In object;
};
夏见 2024-11-14 04:19:44

与继承类似,A 类 的对象嵌入在 B 类 的对象中:

class A {
  int x;
};
class B {
  int y;
  A a1;
  A a2;
};

如果您实例化 B b,您会得到一些东西在您的记忆中,就像这样:

int y; // b.y
int x; // b.a1.x
int x; // b.a2.x

与指针属性相反,嵌套对象实际上位于外部对象内部。此外,您不必担心创建/删除 A,因为创建/删除 B 时会自动完成。

Similar to inheritance, an object of class A is embedded in the object of class B:

class A {
  int x;
};
class B {
  int y;
  A a1;
  A a2;
};

If you would instantiate B b, you would get something like this on your memory:

int y; // b.y
int x; // b.a1.x
int x; // b.a2.x

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 when B is created/deleted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文