【问题标题】:can class inherit struct in C++? [duplicate]类可以继承C++中的结构吗? [复制]
【发布时间】:2013-08-26 13:06:56
【问题描述】:

我在 c++ 中有以下代码:

typedef struct
{
   int a;
   int b;
   float c;
} Data;

class DataInfo : public Data
{
   // some code here
};

我的问题是类可以继承 C++ 中的结构吗?这是怎么回事?

【问题讨论】:

  • 同样的事情?怎么来的?
  • 除了一个类中的所有元素默认都是私有的
  • @user2131316 structclass 之间的唯一区别是 class 的成员默认是 privatestruct 的成员是 public默认情况下。继承也是如此
  • 请不要使用讨厌的typedef struct {} Name 构造。这是丑陋和不必要的!
  • 这是不必要的,因为它完全没有。改用struct Name {};

标签: c++ struct inheritance


【解决方案1】:

struct 包含在 C++ 中以提供与 C 的兼容性。它具有与 class 相同的功能,但结构中的成员默认为 public。所以你可以从struct 继承class 并反过来做同样的事情。

【讨论】:

    猜你喜欢
    • 2011-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 2017-04-13
    • 2023-03-06
    • 2020-05-24
    • 1970-01-01
    相关资源
    最近更新 更多