C++ 中的类成员隐私和标头
所以我正在创建一个类来定义 D&D 中的角色。我认为设置类的方式是公共成员在标头中定义,私有成员在 .cpp 中定义,这样它们就不会向外部公开,对吗?你如何做到这一点?目前看起来像这样,我确信这是错误的。
character.h:
namespace d20 {
class character {
public:
character(void);
virtual ~character(void);
// get stats
int getStr();
int getDex();
int getCon();
int getIntl();
int getWis();
int getCha();
// get modifiers of the stats
int getStrMod();
int getDexMod();
int getConMod();
int getIntlMod();
int getWisMod();
int getChaMod();
};
};
character.cpp:
namespace d20 {
class character {
private:
int str; // strength
int dex; // dexterity
int con; // constitution
int intl; // intelligence
int wis; // wisdom
int cha; // charisma
int hp; // hit points
int ac; // armor class
};
character::character(void) {
}
character::~character(void) {
}
}
是的,我知道缺少很多内容,但这不是重点。 Visual Studio 2010 在类视图中将其显示为 2 个名为“字符”的类。我如何实现我的预期?这并不重要,因为它是一个分配,并且所有代码都是可见的,但对于将来,将私有成员保留在 .h 文件之外可能是明智的做法?
So I'm making a class to define a character in D&D. The way I thought setting up the class was that public members are defined in the header and private in the .cpp so they're not revealed to the outside right? How do you do this? Currently it looks like this and I'm sure it's wrong.
character.h:
namespace d20 {
class character {
public:
character(void);
virtual ~character(void);
// get stats
int getStr();
int getDex();
int getCon();
int getIntl();
int getWis();
int getCha();
// get modifiers of the stats
int getStrMod();
int getDexMod();
int getConMod();
int getIntlMod();
int getWisMod();
int getChaMod();
};
};
character.cpp:
namespace d20 {
class character {
private:
int str; // strength
int dex; // dexterity
int con; // constitution
int intl; // intelligence
int wis; // wisdom
int cha; // charisma
int hp; // hit points
int ac; // armor class
};
character::character(void) {
}
character::~character(void) {
}
}
Yes I know a ton is missing, but that's besides the point. Visual Studio 2010 shows it as 2 classes named character in class view. How do I accomplish what I intended? It doesn't matter much for this since it's an assignment and all the code is visible anyway, but for the future it'd probably be smart to keep private members out of the .h file correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需将
private
成员放在同一个头文件中即可。根据设计,类定义不能分布在不同的文件中。为什么您首先要阻止客户看到这些
private
成员?由于它们是私有
,因此无论如何都无法直接访问它们。编辑:
这些私有成员是类的实现细节。它们必须出现在类的定义中,因为有时编译器需要知道类对象的大小:
在上面的示例中,编译器无法知道 ch 的大小,除非它看到
字符
的定义,并且它可以通过查看类定义来计算出大小!这就是为什么需要将实现放入类定义中的原因之一。Just put
private
members in the same header file. Class definition cannot be spread across different files by design.Why do you want prevent those
private
members from being seen by clients in the first place? Beingprivate
, they cannot be accessed directly anyway.Edit:
Those private members are implementation details of the class. They are necessary to be in the definition of the class because sometimes compiler needs to know the size of objects of the class:
In the above example, the compiler has no way to know the size of
ch
unless it sees the definition ofcharacter
, AND it can figure out the size by looking at the class definition! That's one reason why you need put implementations in the class definition.将这些私有声明移至头文件可能是个好主意。
Might be a good idea to move these private declarations to the header file.
您首先声明 2 个类。
.h 中的一个1 在 .cpp 中同名。你认为它会编译吗?
此外,将事物设为私有意味着其他类和其他类将无法访问它。有物体。
&你认为如果一个人可以这样看到你的.h,他将无法看到你的.cpp吗?
.h & .cpp 不是单独的东西。他们最终形成了一个单一的实体。在编译期间,所有标头内容都会合并到 .cpp 文件中。 &所有.cpp最终变成dll或exe或机器可以理解的东西&跑步。 .h 的形成只是为了使代码更简洁。
在标头中将其设为私有,这样会更干净。
you are declaring 2 classes first of all.
one in .h & 1 in .cpp & with the same name. Do you think it will even compile ?
Also Making a thing private means it will not be accessible to other classes & there objects.
& do you think if a person can see your.h like this he will not be able to see your .cpp?
.h & .cpp are not separate thing. They are finally forming a single entity. During compilation all header contents get merged in .cpp file. & all .cpp finally into a dll or exe or something which machine can understand & run. .h is formed just to make code cleaner.
Make it private in header it will be more cleaner.