C++:另一个类中的类作为类型?

标签 c++ class inheritance private

谁能给我解释一下这种可以在 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;
}

最佳答案

这不是继承,因为Y 不是从X 派生的。

这只是简单的封装X x只是Y的成员变量,和char c_X的成员变量没什么区别。

关于C++:另一个类中的类作为类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44572369/

相关文章:

c++ - 为什么函数对象应该是按值传递的

c++ - 在程序中使用 boolean 函数时遇到问题

c++ - 如果 operator<< 重载,VSCode 表示 std::chrono 不明确

python - python 3中类 'bytes'的不同表示

php - 如何在特定路径中加载 PHP 类?

PHP 单例和继承

c++ - 调用隐式删除的默认构造函数

java - 将一个对象的引用传递给另一个类中的另一个方法

inheritance - DDD : aggregate root specialization

c++ - 我们在 C++ 中使用 "inherit"构造函数吗? "inheriting"的确切定义是什么