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;
}