0

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;
}
Ryad Kovach
  • 29
  • 1
  • 7
  • 7
    It's the same as `private: char c_;`. It's just a member variable. Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Jun 15 '17 at 16:18
  • @NathanOliver so it is just synonym ? Has nothing to do with properties from class X ? – Ryad Kovach Jun 15 '17 at 16:21
  • No it is not a synonym. It declares a class member named `x` that has the type of `X`. Just like `private: char c_;` declared a class member named `c` of type `char`. – NathanOliver Jun 15 '17 at 16:22
  • https://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor – TallChuck Jun 15 '17 at 16:23

1 Answers1

1

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.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770