gpt4 book ai didi

c++ - 复制基类的构造函数

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

<分区>

我知道 stackoverflow 上有很多关于复制构造函数的例子。但是,我还没有看到我遇到的具体问题。

假设我有一个基类 Base 定义如下:

class Base
{
public:
Base(const Base &other);
...
}

我有一个派生类 Derived 定义如下:

class Derived
{
public:
Derived(const Base &other); //construct from base class
Derived(const Derived &other); //construct from derived class (copy constructor)
}

复制构造函数可以这样定义吗?

Derived::Derived(const Derived &other) : Base(other) {...}

或者这在 C++ 中不是很好的风格吗?如果不是,我怎样才能使用更好的方法获得相同的结果?我的编译器 (MSVC) 似乎没有提示它。

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