Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 继承和复制构造函数-如何从基类初始化私有字段?_C++_Inheritance_Copy Constructor - Fatal编程技术网

C++ 继承和复制构造函数-如何从基类初始化私有字段?

C++ 继承和复制构造函数-如何从基类初始化私有字段?,c++,inheritance,copy-constructor,C++,Inheritance,Copy Constructor,我有两门课,A和B。在A中,我有3个私有字段。在类B中,我想编写一个复制构造函数,并从类a初始化私有字段。但是,这不起作用: #include <iostream> #include <string> using namespace std; class A { private: string *field1; string *field2; string *field3; double num1

我有两门课,
A
B
。在
A
中,我有3个私有字段。在类
B
中,我想编写一个复制构造函数,并从类
a
初始化私有字段。但是,这不起作用:

#include <iostream>
#include <string>
using namespace std;

class A
{
    private:

        string *field1;
        string *field2;
        string *field3;
        double num1;

    public:

        A(string *o, string *n, string *m, double a=0)
        {
            field1 = new string(*o);
            field2 = new string(*n);
            field3 = new string(*m);
            num1 = a;
        }

        A(const A& other) {
            field1 = new string(*other.field1);
            field2 = new string(*other.field2);
            field3 = new string(*other.field3);
            num1 = other.num1;
        }

        void show()
        {
            cout << *field1 << " " << *field2 << " " << *field3 << "\n";
        }

        ~A()
        {
            delete field1;
            delete field2;
            delete field3;
        }
};

/*--------------------------------------------------------------------------------------------*/

class B : public A
{
    private :

        double num2;
        double num3;

    public:

        B(double num2, double num3, string *o, string *n, string *num, double a=0) : A(o,n,num,a)
        {
            this->num2 = num2;
            this->num3 = num3;
        }

        B(const B& other) : A(other.field1, other.field2, other.field3, other.num1)
        {
            num2 = other.num2;
            num3 = other.num3;
        }

        void show()
        {
            cout << num2 << " " << num3 << "\n";
            A::show();
        }
};

int main()
{
    string o = "TEXT 111";
    string *optr = &o;

    string n = "TEXT 222";
    string *nptr = &n;

    string *numptr = new string("9845947598375923843");

    A ba1(optr, nptr, numptr, 1000);
    ba1.show();

    A ba2(ba1);
    ba2.show();

    A ba3 = ba2;
    ba3.show();

    B vip1(20, 1000, optr, nptr, numptr, 3000);
    vip1.show();

    B vip2(vip1);
    vip2.show();

    delete numptr;
    return 0;
}

您需要从复制构造函数中调用父复制构造函数,并赋予它相同的参数。

当您复制构造函数
B
时,只需调用
A
的复制构造函数

B(const B& other) : A(other)
{
    num2 = other.num2;
    num3 = other.num3;
}
由于
B
继承自
A
这是合法的,
A
将复制
A
other
部分

还要注意的是,所有这些指针都是不必要的,这使得代码更加复杂。我们可以像这样重写它:

class A
{
private:
    string field1;
    string field2;
    string field3;
    double num1;

public:
    A(const string& o, const string& n, const string& m, double a = 0) : field1(o), field2(n), feild3(m), num1(a) {}

    A(const A& other) field1(other.field1), field2(other.field2), feild3(other.feild3), num1(other.num1) {}

    void show()
    {
        cout << field1 << " " << field2 << " " << field3 << "\n";
    }
};

/*--------------------------------------------------------------------------------------------*/

class B : public A
{
private:

    double num2;
    double num3;

public:

    B(double num2, double num3, const string& o, const string& n, const string& m, double a = 0) : A(o, n, num, a), num2(num2), num3(num3) {}

    B(const B& other) : A(other), num2(other.num2), num3(other.num3) {}

    void show()
    {
        cout << num2 << " " << num3 << "\n";
        A::show();
    }
};
A类
{
私人:
字符串字段1;
字符串字段2;
字符串字段3;
双num1;
公众:
A(常量字符串&o,常量字符串&n,常量字符串&m,双A=0):field1(o),field2(n),feild3(m),num1(A){
A(const A&other)field1(other.field1)、field2(other.field2)、feild3(other.feild3)、num1(other.num1){}
无效显示()
{

cout当使用类B的复制构造函数时,应该调用类A的复制构造函数

因此,用以下代码替换您的代码:

    B(const B& other) : A(other)
    {
        num2 = other.num2;
        num3 = other.num3;
    }

另外,将类A中的析构函数声明为virtual方法。

按照您的建议将它们设置为
受保护的
,然后从B的ctor主体访问它们。将它们设置为
受保护的
有什么问题吗?如果您无法更改
A
,那么您所要求的是不可能的。而且您的代码正在泄漏并且不是异常安全的。为什么要使用指向字符串的指针?@RJFalconer:我很好奇,什么t我需要从子类初始化
private
字段。正如我们所看到的,对于初始化列表,它不需要work@ChristianHackl,当然有可能!请看我的答案。(指向字符串部分的指针当然有效)@ChristianHackl,OP明确接受了下面的答案,并解释说他们完全理解这个问题。OP不需要访问字段,只需要确保在调用子复制ctor时初始化字段,并且不知道如何直接调用基本复制ctor。
A
如何知道它应该复制的
其他
的哪一部分这是因为我在
a
中有一个复制构造函数?@yak(这里不是问题)。@LogicStuff:谢谢you@yak:
A
不知道。但编译器知道。如果
A
未实现复制构造函数(显式或隐式),则编译器在尝试编译
A(其他)
时将发出错误。
    B(const B& other) : A(other)
    {
        num2 = other.num2;
        num3 = other.num3;
    }