带有派生参数的基础c'tor

时间:2010-10-03 10:50:41

标签: c++

我创建了一些课程:

class Base{
public:
 Base(int = 0);
 ~Base();
 Base(Base&);
 Base(Derived&); //<- here
 int valueOfBase();
protected:
 int protectedData;
private:
 int baseData;
};

class Derived: public Base{
public:
    Derived(int = 0);
    Derived(Derived&);
    ~Derived();
private:
    int derivedData;
};

###//Impplementation in .cpp

Base::Base(Base& base)
{
    baseData = base.baseData;
    protectedData = base.protectedData;
    cout << "base Copy C'tor" << endl;
}

Base::Base(Derived& hello)
{
    baseData = hello.protectedData;
}

但是当我尝试编译一些代码时,我会收到一个错误(靠近here

expected ) before & token

我可以创建带有派生参数的base c'tor来编写像这样的东西

Derived derived(1);
Base base1 = derived;

如果没有原因? 提前谢谢

1 个答案:

答案 0 :(得分:1)

您可能需要Derived的正向声明。

尝试在基类定义上方添加class Derived;