【发布时间】:2013-11-18 19:14:32
【问题描述】:
我想知道如何将目标 C 中的“typedef struct”转换为 c++ 代码,最好将其转换为类。是否可以 ?我可以使用类吗?
例子:
typedef struct{
int one;
int two;
}myStruct;
【问题讨论】:
我想知道如何将目标 C 中的“typedef struct”转换为 c++ 代码,最好将其转换为类。是否可以 ?我可以使用类吗?
例子:
typedef struct{
int one;
int two;
}myStruct;
【问题讨论】:
C++ 中struct 和class 之间没有根本区别*。这是具有两个公共数据成员的 mystruct 类型:
struct mystruct {
int one;
int two;
};
这与
完全一样class mystruct
{
public:
int one;
int two;
};
* 不同之处在于成员和基类默认在struct 中是公共的,而在class 中是私有的。这两个关键字可以用来表示相同的类型。
【讨论】:
class Foo { int i; }; 和struct Foo { private: int i; }; 之间的区别。
class 类型的成员默认为 PRIVATE,而struct 成员默认为 PUBLIC。
首先,所有关于阅读一些 C++ 好书的标准内容都适用。
在 C++ 中,结构是类,唯一的区别是类成员的默认可见性并且这些对象不需要 typedef,myStruct 可以自动使用,就像您对它进行 typedef'd 一样。因此,您将拥有:
struct myStruct {
int one;
int two;
};
并且您可以在此之上添加成员函数和所有这些。普通旧数据 (POD) 结构在 C++ 中也很好。
【讨论】: