|
|
question about C++
|
|
In the following program, What's the problem in the Object B ( the sentence "b=100" )?
class A {
public:
int i;
void operator = ( int a ) { i=a; }
void operator += ( int a ) { i+=a; }
};
class B : public A {
};
void main() {
A a;
a=100;
a+=100;
B b;
b=100;
b+=100;
}
/temp/a.cpp: In function `int main(...)':
/temp/a.cpp:17: no match for `B & = int'
/temp/a.cpp:9: candidates are: B::operator =(const B &)
Why there isn't the same problem in the Object A?
And why the sentence "b+=100" is not the problem ?
|
|
|
Re: question about C++
|
|
如果我没有记错的话,运算符重载不能继承!!!!
|
|
|
Re: question about C++
|
|
呵呵,记错了。对于赋值符“=”比较特殊,因为它和拷贝构造函数的功能重叠,不能够被继承。(我的理解,有待考证)
对于其他的运算符继承是起作用的。
|
|
|
Re: question about C++
|
|
因为它和拷贝构造函数的功能重叠
错!
在C++里,
myclass A;
myclass B=A; // 调用 copy 构造函数
A=B; // 调用 operator =( const myclass &)
日知其所亡
|
|
|
Re: question about C++
|
|
同意!
|
|
|
Re: question about C++
|
|
开始说错了,应该说"因为它和构造函数的功能有重叠之处 ".
|
|
|
Re: question about C++
|
|
说的还是不够明白易懂,应该说是:
赋值操作符完成类似于constructor的功能,而constructor不能被继承,所以赋值操作符也不能被继承。
|
|
|
Re: question about C++
|
|
我想不是这个原因
A& operator =(const A&)
可以被B继承
日知其所亡
|
|
|
Re: question about C++
|
|
错了,=运算符不能被继承,
你说的也是,
事实上=运算符都是返回该类的引用,
b=100
要求一个返回B&的=运算符,
A里面哪有,
呵呵
Linux初级研究员
|
|
|
Re: question about C++
|
|
运算符重载的规则
1.友元运算符的参数规则与类成员运算符的参数规则不同, 一员运算符必须显式地声明一个参数, 二员运算符必须显式地声明两个参数. 类成员运算符重载时, 参数中隐含了一 个this指针.
2. 重载运算符不能改变原有运算符的优先级, 结合性和操作数个数.
3. 重载运算符不能使用缺省参数.
4. 除赋值运算符外, 重载运算符可由派生类继承下去.
5. 运算符=、()、[]和->可作为类成员运算符, 不能作为友员运算符.
6. 运算符“.”、“::” 、“?:” 不能重载.
http://www.cbi.pku.edu.cn/Doc/teach/cpp/cpp11/tsld015.htm
日知其所亡
|
|
|
Re: question about C++
|
|
看看这个:
1 #include "iostream.h" 2 3 class A 4 { 5 public: 6 int m; 7 8 A(int l=0){m=l;} 9 10 A&operator=(const A&a); 11 A&operator=(const int i); 12 }; 13 14 A&A::operator=(const A&a) 15 { 16 m=a.m+800000; 17 return *this; 18 } 19 20 A&A::operator=(const int i) 21 { 22 m=i+4000000; 23 return *this; 24 } 25 26 class B:public A 27 { 28 public: 29 int l; 30 31 B(int m=0):A(m){l=999;}; 32 }; 33 34 35 int main(void) 36 { 37 A a; 38 a=90; 39 cout<<a.m<<endl; 40 41 B b; 42 B b1(445); 43 b=b1; 44 45 cout<<b.m<<endl; 46 47 B b3; 48 b3=90; 49 return 1; 50 }
\运行结果为: 4000090 800445 90 redhat 7.2 g++ 3.0.2 编译通过 我猜测派生类在"="会调用基类的Base&operator=(const Base&),而没有把"=" "继承"(比如Base&operator=(int) 就不能用了 )
|
|
|
Re: question about C++
|
|
我也测试过了一些和你类似的例子,都没有通过,说明问题不是没有A& operator=(A&)问题,但你的程序给我提了个醒,呵呵,终于也长了见识。
看下面的程序:
一、类的声明
1、完整的类声明
class A {
public:
int i;
A(int a=0){i=a;}
A &operator = ( int a ) { i=a;cout<<"i="<<i<<'\n'; }
void operator += ( int a ) { i+=a;cout<<"i="<<i<<'\n'; }
// void operator = ( const A &a) { i=a.i;cout<<"i="<<i<<'\n';}
};
class B : public A {
public:
B(int a=0):A(a){}
};
2、无显式构造函数
二、main函数
#include <iostream>
using namespace std;
{此处是类声明}
int main() {
A a;
a=100;
a+=100;
B b;
// b=a;
b=100;
b+=100;
}
使用gcc3.0.3 平台mdk8.1
第一种情况,类声明完整时,编译通过,执行结果:
i=100
i=200
i=200
也就说说,b=100调用的是构造函数!
第二种情况,无构造函数,情况编译不通
诸位可以取消=重载,改变构造函数的存取控制,
等等,都可以得到有趣的结果,不过上例
谁想明白了,给总结总结
Linux初级研究员
|
|
|
Re: question about C++
|
|
这显然说明了=重载操作符是不被继承的
Linux初级研究员
|
|
|
Re: question about C++
|
|
en ,你这个例子中b=b1倒是调用的
A的A&operator=(A&)构造函数,
为什么=(int)就不能继承呢??
Linux初级研究员
|
|
|
Re: question about C++
|
|
以前看过的,记得可能不清楚。
C中的结构赋值是简单的内存拷贝,不必细说。
C++中一个对象赋值给同类对象,将调用“赋值函数”,即C& C::operator=(C&)形式的成员函数。如果没定义此函数,编译器自动生成一个赋值函数:调用基类的赋值函数,然后对每一个成员赋值,成员为对象时,赋值会调用其相应的赋值函数。
所以基类的赋值函数虽不能继承,但是可能被隐式调用。
当然在最简单的情况下,赋值函数可以简化为内存拷贝。
|
|
|
Re: question about C++
|
|
有些道理
日知其所亡
|
|
|
Re: question about C++
|
|
嗯,这个解释满合理
Linux初级研究员
|