Max*_*Max 7 c++ inheritance variable-assignment
我不太明白为什么对于赋值,派生类不会调用基类的相应运算符,如果它自己不存在的话.看看代码:
#include <iostream>
using namespace std;
class A{
protected:
void myWrite(){
cout << " Base " <<endl;
}
public:
double x,y;
A(): x{0},y{0}{};
virtual A & operator=(double g){x=g;y=g;return *this;}
virtual ~A(){};
virtual void doneit(){myWrite();}
};
class B: public A{
protected:
public:
B():A(){};
virtual ~B(){};
virtual void doneit(){myWrite();}
};
int main() {
A jj;
B pp;
pp=0.0;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
因为它是代码不编译.当然,如果我为B定义一个"运算符="与A的相同,那么一切正常,但是为什么默认情况下不调用B"operator ="如果未定义派生类中的那个?你能帮忙解释一下这个问题吗?
gcc编译器说../src/tito.cpp:40:4:错误:没有可行的重载'='pp = 0.0; ~~ ^ ~~~ ../src/tito.cpp:28:7:注意:候选函数(隐式复制赋值运算符)不可行:对于第一个参数类B,没有已知的从'double'到'const B'的转换:public A {^ 1错误生成.
你能解释一下为什么它不起作用吗?
Bar*_*rry 13
如果您没有声明复制赋值运算符,编译器将为您声明一个.所以,你的类B 确实是这样的:
class B : public A {
public:
// explicit
B();
virtual ~B();
virtual void doneit();
// implicit
B(const B&);
B& operator=(const B&);
};
Run Code Online (Sandbox Code Playgroud)
隐式拷贝赋值运算符隐藏的A::operator=(double),你写的,所以它的这个名字查找将找到的唯一候选人.当然,它不是一个可行的候选人,因为double不可转换B,因此错误.
要使用A::operator=(double )运算符,您必须明确地将其纳入范围:
using A::operator=;
Run Code Online (Sandbox Code Playgroud)
但是那时你不会分配任何B部分.所以最好更明确一点:
B& operator=(double g) {
// B stuff here
// A stuff
A::operator=(g);
return *this;
}
Run Code Online (Sandbox Code Playgroud)
编译器隐式声明的派生类的复制赋值运算符隐藏了基类的赋值运算符。按以下方式在派生类中使用 using 声明
class B: public A{
protected:
public:
using A::operator =;
B():A(){};
virtual ~B(){};
virtual void doneit(){myWrite();}
};
Run Code Online (Sandbox Code Playgroud)
另一种方法是在派生类中重新声明虚拟赋值运算符
B & operator=(double g) { A::operator =( g ) ;return *this;}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您可以使用多态性。例如
#include <iostream>
class A
{
public:
double x,y;
A(): x{0}, y{0} {}
virtual A & operator =( double g ){ x = g; y = g; return *this; }
virtual ~A(){}
};
class B: public A
{
public:
B() : A(){}
virtual B & operator =( double g ) override { A::operator =( 2 * g ) ; return *this; }
virtual ~B(){}
};
int main()
{
A a;
B b;
A *p = &a;
*p = 10;
std::cout << p->x << ' ' << p->y << std::endl;
p = &b;
*p = 10;
std::cout << p->x << ' ' << p->y << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
程序输出是
10 10
20 20
Run Code Online (Sandbox Code Playgroud)