Ryad Kovach
Ryad Kovach

Reputation: 29

C++: Class within another class as type?

could someone explain me this kind of "inheritance" which can be found in class Y: private?

class X
{
  private: char c_;
  public: X(char c) : c_(c){}
};

class Y
{
  private: X x_; // What is this ?
  public: Y(X x): x_(x){}
};

int main()
{
  X m('a');
  Y *test = new Y(m);

  delete test;
  return 0;
}

Upvotes: 0

Views: 56

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595377

This is not inheritance, as Y does not derive from X.

This is just simple encapsulation. X x is just a member variable of Y, no different than char c_ being a member variable of X.

Upvotes: 1

Related Questions