What is a container ship or nesting in C ++?

I understood: Containership: the class contains other class objects as element data.

please explain an example.

Thank.

+3
source share
4 answers

Class nesting is just a class defined in another class, for example:

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

};

Then you can access the nested class using the scope operator, for example, with a namespace:

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

Now that the class contains another object class , it is aggregation :

class D
{
public:

   A myA;

   void do_something();

private:
   A::B myB;

};

Then you can access this element if it is open:

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

, . :

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

.

:

class Contained
{
    int foo;
};

class Container
{
    Contained bar;
};

foo :

Container c;
c.bar.foo = 42;

. , :

class AngryMammoth
{
    class CrazyVulture
    {
        int legCount;
    };

    int numberOfPeopleKilledSoFar;
};
+3
class In {};
class Out {
  In object;
};
+2

class A class 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 /.

-1

All Articles