【发布时间】:2015-02-22 23:32:47
【问题描述】:
可以私下调用拥有其成员的父构造函数吗? 我知道受保护的可以,但我更喜欢使用私有,有什么办法可以解决这个问题吗?
class Product {
std::string id;
std::string description;
double rateIVA;
public:
Product(std::string id, std::string description, double rateIVA);
~Product();
// Abstract Methods / Pure Virtual Methods
virtual double getIVAValue() = 0;
virtual double getSaleValue() = 0;
// Virtual Method
virtual void print() const;
// Setters & Getters
void setId(std::string id);
std::string getId() const;
void setDescription(std::string description);
std::string getDescription() const;
void setRateIVA(double rateIVA);
double getRateIVA() const;
};
class FixedPriceProduct : protected Product {
double price;
public:
FixedPriceProduct();
FixedPriceProduct(double price); // Implement here
~FixedPriceProduct();
double getIVAValue();
double getSaleValue();
virtual void print() const;
};
【问题讨论】:
-
我不明白这个问题。你有一个
Product(std::string id, std::string description, double rateIVA);,它填充了产品的三个成员变量(至少我认为是这样),并且它是公开的。只是在子构造函数的初始化列表中调用它? (子构造器需要更多的params来获取父级的值,但是private没有问题) -
私有继承也可以,看这个问题:stackoverflow.com/questions/15042921/…
-
@deviantfan:你什么意思?该链接与私有构造函数无关。
-
@swang 啊抱歉,不知道我在那里读到了什么