gpt4 book ai didi

c++ - 具有派生参数的基本c'tor

转载 作者:行者123 更新时间:2023-11-28 08:26:37 26 4
gpt4 key购买 nike

我创建了一些类:

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;
}

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

expected ) before & token

我可以创建带有派生参数的基础 c'tor 来编写类似的东西吗

Derived derived(1);
Base base1 = derived;

如果没有,为什么?提前致谢

最佳答案

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

尝试在您的基类定义之上添加 class Derived;

关于c++ - 具有派生参数的基本c'tor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3849451/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com