让我们有一个 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;
溴化氢
#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.
}