C ++オブジェクトAを作成しましょう。Aには、その子がアクセスできる2つの変数(VAR1とVAR2)があります。オブジェクトBはAを拡張し、1つのプライベート変数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.
}