C++变量 - 声明和定义。遗产

发布于 2024-10-15 14:25:00 字数 883 浏览 2 评论 0原文

让我们有一个 C++ 对象 A。A 中有两个变量(VAR1 和 VAR2)可供其子对象访问。 对象 B 扩展了 A 并具有一个私有变量 VAR3,它还可以访问 VAR1 和 VAR2。 A/B 的每个实例都有自己的变量。

这是声明和定义变量的正确方法吗?


class A {
protected:
    static std::string const VAR1;
    static std::string VAR2;
};

A.cpp


#include "A.h"
using namespace std;
string const A::VAR1 = "blah";
string A::VAR2;

Bh


#include "A.h"
class B : public A {
private:
    static std::string VAR3;

public:
    B(std::string const v1, std::string const v2);
    void m() const;
};

B.cpp


#include "B.h"
using namespace std;

string B::VAR3;

B::B(string const v1, string const v2) {
    VAR2 = v1;
    VAR3 = v2;
}

void B::m() const {
    // Print VAR1, VAR2 and VAR3.
}

Let's have a C++ object A. There are two variables (VAR1 and VAR2) in A accessible to its children.
Object B extends A and has one private variable VAR3 it can also access VAR1 and VAR2. Each instance of A/B has its own variables.

Would this be the right way of declaring and defining the variables?

A.h


class A {
protected:
    static std::string const VAR1;
    static std::string VAR2;
};

A.cpp


#include "A.h"
using namespace std;
string const A::VAR1 = "blah";
string A::VAR2;

B.h


#include "A.h"
class B : public A {
private:
    static std::string VAR3;

public:
    B(std::string const v1, std::string const v2);
    void m() const;
};

B.cpp


#include "B.h"
using namespace std;

string B::VAR3;

B::B(string const v1, string const v2) {
    VAR2 = v1;
    VAR3 = v2;
}

void B::m() const {
    // Print VAR1, VAR2 and VAR3.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

冬天的雪花 2024-10-22 14:25:00

A/B 的每个实例都有自己的变量。

这是声明和定义变量的正确方法吗?

不。您已将 A 的成员声明为 static,这意味着它们是类变量,而不是实例变量。每个实例都没有自己的副本。相反,它们都共享同一个实例。

然后使之成为非静态

class A {
protected:
    std::string const VAR1;
    std::string VAR2;
};

...然后,当然,您不需要全局初始化程序,因此摆脱此:

string const A::VAR1 = "blah";
string A::VAR2;

...如果您想要VAR1 每次实例化 A 时都有一个默认值,那么您可以在类的初始化列表中执行此操作(或者在 ctor 主体中,如果您是朋克:) ):

A::A() : VAR1("blah") {};

Each instance of A/B has its own variables.

Would this be the right way of declaring and defining the variables?

No. You've declared A's members as static which means they are class-variables, not instance-variables. Each instance doesn't get it's own copy. Instead, they all share the same instance.

Make then non-static:

class A {
protected:
    std::string const VAR1;
    std::string VAR2;
};

... and then, of course, you don't need the global initializer so get rid of this:

string const A::VAR1 = "blah";
string A::VAR2;

...and if you want VAR1 to have a default value every time A is instantiated, then you can do that in the class' initializer list (or in the ctor body, if you're a punk :) ):

A::A() : VAR1("blah") {};
默嘫て 2024-10-22 14:25:00

A/B 的每个实例都有自己的变量。

事实并非如此。您已将它们声明为静态。停止这样做,您可能会更接近您想要的结果。

Each instance of A/B has its own variables.

Not so. You've declared them static. Stop doing that and you might get closer to your desired result.

暗喜 2024-10-22 14:25:00

不,你搞错了。

  • 该问题要求每个实例都有自己的变量集,那么为什么要将 AB 的数据声明为 static 呢?
  • 您不需要实施任何事情。问题只是要求您声明类型及其成员数据。
  • 就需求要求而言,构造函数也不是必需的。

No, you got it wrong.

  • The problem requires each instance to have its own set of variables, so why are you declaring A and B's data as static?
  • You don't need to implement anything. The problem just asks you to declare the types and their member data.
  • The constructor isn't necessary either as far as what the requirements ask you to do.
千と千尋 2024-10-22 14:25:00
class A{
protected:
    char var1,var2;
};

class B: public A {
private:
    char var3;
};

void main()
{
    B b1;
    b1.var3='a';
}
class A{
protected:
    char var1,var2;
};

class B: public A {
private:
    char var3;
};

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