C++ 中的类成员隐私和标头

发布于 2024-12-11 10:27:54 字数 1226 浏览 2 评论 0原文

所以我正在创建一个类来定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

年少掌心 2024-12-18 10:27:54

只需将 private 成员放在同一个头文件中即可。根据设计,类定义不能分布在不同的文件中。

为什么您首先要阻止客户看到这些 private 成员?由于它们是私有,因此无论如何都无法直接访问它们。

编辑:

这些私有成员是类的实现细节。它们必须出现在类的定义中,因为有时编译器需要知道类对象的大小:

int main()
{
  character ch;

  // pass by value
  Func(ch);
  ..
}

在上面的示例中,编译器无法知道 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? Being private, 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:

int main()
{
  character ch;

  // pass by value
  Func(ch);
  ..
}

In the above example, the compiler has no way to know the size of ch unless it sees the definition of character, 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.

筑梦 2024-12-18 10:27:54

将这些私有声明移至头文件可能是个好主意。

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

Might be a good idea to move these private declarations to the header file.

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
醉生梦死 2024-12-18 10:27:54

您首先声明 2 个类。

.h 中的一个1 在 .cpp 中同名。你认为它会编译吗?
此外,将事物设为私有意味着其他类和其他类将无法访问它。有物体。
&你认为如果一个人可以这样看到你的.h,他将无法看到你的.cpp吗?

.h & .cpp 不是单独的东西。他们最终形成了一个单一的实体。在编译期间,所有标头内容都会合并到 .cpp 文件中。 &所有.cpp最终变成dll或exe或机器可以理解的东西&​​amp;跑步。 .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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文