我有一个基类和派生类。基类构造函数有一些静态 const 变量。可以在派生类构造函数中使用它来构造基类变量吗?
一个示例代码将是
//Base.hpp
class Base {
public:
Base(int value_, long sizee_);
private:
int value;
int sizee;
protected:
static const int ONE = 1;
static const int TWO = 2;
static const long INT_SIZE = (long)sizeof(int);
static const long LONG_SIZE = (long)sizeof(long);
};
//Base.cpp
Base::Base(int value_,int sizee_):value(value_),sizee(sizee_) {
}
//Derived.hpp
class Derived: class Base {
public:
Derived();
};
//Derived.cpp
Derived::Derived():Base(ONE+TWO,INT_SIZE+LONG_SIZE) {
}
这里 ONE,TWO,INT_SIZE,LONG_SIZE 是基类静态变量,我将使用它来构造基类本身。这种方法好吗?请指教。