但是,一些使用 C 和 C + + 编写代码的商店会使用“ class/struct”来表明哪些代码可以在 C 和 C + + (struct)中使用,哪些代码只能在 C + + (class)中使用。换句话说,在这种风格中,所有的结构都必须与 C 和 C + + 一起工作。这就是为什么很久以前 C + + 还被称为“带有类的 C”的时候会有所不同的原因
请注意,C 联合与 C + + 一起工作,但不是反过来
union WorksWithCppOnly{
WorksWithCppOnly():a(0){}
friend class FloatAccessor;
int a;
private:
float b;
};
struct Pair { // the members can vary independently
string name;
int volume;
};
// but
class Date {
public:
// validate that {yy, mm, dd} is a valid date and initialize
Date(int yy, Month mm, char dd);
// ...
private:
int y;
Month m;
char d; // day
};