开发者

Inherited member from an abstract class can't be initialized by the inherited constructor

class CarPart
{
public:
    CarPart(): na开发者_如何学编程me(""), price(0) {}
    virtual int getPrice() = 0;//{return price;}
protected:
    int price;
    string name;
};

class Tire: public CarPart
{
public:
    virtual int getPrice() {return price;}
    Tire(): CarPart(), name("Tire"), price(50)
    {}
};

Visual 2010 tells me name and price are not members of deriv, but they are inherited (error c2614). What am I doing wrong ?


You cannot initialize members that is not an immediate member of your class. n is not an immediate member of deriv, it's an immediate member of base.

However, n is accessible to deriv, so you can always assign it in your deriv constructor, but you should really initialize it in the base constructor.

Also, you can't have virtual constructors. Did you mean to use virtual destructors?

class base
{
public:
    base() : n(0) {} // Are you sure you don't want this?
    virtual void show() = 0;
protected:
    int n;
};

class deriv : public base
{
public:
    deriv() 
    {
        n = 0;
    }

    virtual void show() {}
};

EDIT (A response to the OP's edit): You don't need virtual methods for this:

class CarPart
{
public:
    CarPart(const char* newName, int newPrice) : name(newName), price(newPrice) {}

    const std::string& GetName() const { return name; }
    int GetPrice() const               { return price; }
private:
    std::string name;
    int price;
};

class Tire : public CarPart
{
public:
    Tire() : CarPart("Tire", 50) {}
};

Assuming that all your CarParts has to have a name and a price, this should be more than sufficient.


1) n is left uninitialized in base;

2) virtual deriv(): n(0) {} is not a constructor

You probably wanted:

class base
{
public:
   base(int n) : n(n) {}

...

class deriv: public base
{
public:
deriv(): base(0) {}
...


Note, constructors can't be virtual!

You can't initialize members of a base class using the member initialization syntax. Either initialize n in the base class (which is the correct way to do it):

class base 
{ 
public: 
    base(int n_ = 0) : n(n_) {}
    virtual void show() = 0;  
protected: 
    int n; 
}; 

class deriv: public base 
{ 
public: 
    deriv() : base(0) {} 
    void show() {} 
}; 

or assign a value to n inside the constructor of the derived class (not good, at least not without initialization in the base class:

class base 
{ 
public: 
    base(int n_ = 0) : n(n_) {}
    virtual void show() = 0;  
protected: 
    int n; 
}; 

class deriv: public base 
{ 
public: 
    deriv() { n = 1; } 
    void show() {} 
}; 


You can't initialize a base class member in a derived class, since the base class is already constructed before the derived class is initialized. You could provide a constructor that accepts a parameter for the value of n:

base(int val) : n(val) {}

Then delegate to it in your deriv constructor:

deriv() : base(0) {}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜