gpt4 book ai didi

c++ - 继承行为问题

转载 作者:行者123 更新时间:2023-11-30 02:07:37 24 4
gpt4 key购买 nike

我一直在阅读有关继承的内容,但我仍然没有 100% 清楚它,这是我的问题:例如,我有一个充满东西的类:

class Something
{
public:
Something();
Something(int A);
Something(Something S);
~Something();

Something& operator=(const Something & s);
Something operator+(const Something & s) const;
// more methods ...
protected:
int A,B,C;
}

所有方法都在某个地方实现,现在我只想要一个新类来添加一个属性和一个方法,但我仍然想要与父类(super class)相同的行为(构造函数、析构函数、方法、运营商),所以我做这样的事情:

class SomethingMore : public Something
{
public:
void OnlyMethodHere();
private:
int D;
}

但我得到类似的东西:

undefined reference to SomethingMore::SomethingMore(int)

是否可以告诉编译器像在 SuperClass 中一样使用所有内容,还是我必须在每个子类的方法中创建一个 SuperClass::method()?

最佳答案

您不必为方法重复。但是对于构造函数,您必须在子类中正确定义它们。无论如何,除非您有任何额外的东西,否则代码是样板。

SomethingMore::SomethingMore(int i_):Something(i_)
{
}

关于c++ - 继承行为问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7613158/

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