gpt4 book ai didi

c++ - C++中的私有(private)继承以及如何重用但修饰基类的运算符?

转载 作者:行者123 更新时间:2023-11-28 02:05:09 25 4
gpt4 key购买 nike

<分区>

我有一个基类和一个名为 Something 的“子”类,它使用私有(private)继承来重用生成的代码。

class Base {
protected:
Base();
public:
virtual ~Base(){}
void cool();
Base& operator=(const Base& other);
};
class Something : private Base {
public:
Something();
void cool(){
// own stuff
// call method from base
Base::cool();
// own stuff
}

Something& operator=(const Something& other){
if (this != &other){
// do own things

现在在这里,在修饰之间,应该是从基类调用 operator= 。但我不确定我应该如何以正确的方式做到这一点。我应该使用dynamic_cast 是这样的:

              (dynamic_cast<Base>(*this)).operator =(dynamic_cast<Base>(other));
// do own things
}
return *this;
}

}

最好的问候。

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