gpt4 book ai didi

c++ - 预定义派生类

转载 作者:行者123 更新时间:2023-11-30 00:58:41 26 4
gpt4 key购买 nike

我正在尝试按如下方式预定义派生类,

class Base{

}

class Derived;

class Unreleated{
Base* ptr;
public:
Unreleated& operator, (const Unreleated& m){
static_cast<Derived*>(ptr); // pointer is a derived.

return *this;
}


}

class Derived : public Base{
//this is a container holding a bunch of unrelated.
}

但是我得到了从 Base* 到 Derived* 的无效静态转换?

最佳答案

Unreleated::operator,的定义移到Derived的定义之后。

class Base{

};

class Derived;

class Unreleated{
Base* ptr;
public:
Unreleated& operator, (const Unreleated& m);

};

class Derived : public Base{
//this is a container holding a bunch of unrelated.
};

Unreleated& Unreleated::operator, (const Unreleated& m){
static_cast<Derived*>(ptr); // pointer is a derived.

return *this;
}

关于c++ - 预定义派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5820716/

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