它会调用哪个函数?
这是代码,我写了注释。 问题是我不知道 Derive 类中的函数取消隐藏后将调用哪个函数。
#include <CONIO.H>
#include <IOSTREAM>
#include <string>
using namespace std;
class Base
{
string strName;
public:
Base& operator=(const Base &b)
{
this->strName = b.strName;
cout << "copy assignment" << endl;
return *this;
}
Base& operator=(string& str)
{
this->strName = str;
cout << "operator=(string& str)" << endl;
return *this;
}
};
class Derive : public Base
{
public:
int num;
using Base::operator =; // unhide Base::operator=();
};
int main(int argc, char *argv[])
{
Derive derive1;
derive1.num = 1;
Derive derive2;
Base b1;
derive1 = b1; // This will call Base& Base::operator=(const Base &b)
//no problem
string str("test");
derive1 = str; // This will call Base& Base::operator=(string& str)
// no problem
derive2 = derive1; // What function will this statement call???
// If it calls Base& Base::operator(const Base &b)
// how could it be assigend to a class Derive?
return 0;
}
但代码的结果是:derive2.num等于1!!!,这意味着语句后整个类都被复制了,为什么会出现这种情况呢?
感谢托尼,我想我得到了答案。
这是我的解释:
基于 C++0x 7.3.3.3 和 12.8.10 ,Derive
中的using-statement会这样解释
class Derive : public Base
{
public:
int num;
//using Base::operator =;
Base& operator=(const Base &b); // comes form the using-statement
Base& operator=(string& str); // comes form the using-statement
Derive& operator=(const Derive &); // implicitly declared by complier
};
所以当我写:
string str("test");
derive1 = str;
function Base& Base::operator=(string& str);
将被调用,
当我编写:
Base b1;
derive1 = b1;
function Base& 时,最终将调用 Base::operator=(const Base &b);
当我编写:
derive2 = derive1;
function Derive& Dervie::operator=(const Derive&);
将被调用。
Here is the code, I wrote the comments.
The question is I don't know which function will be called after the function unhide in the Derive class.
#include <CONIO.H>
#include <IOSTREAM>
#include <string>
using namespace std;
class Base
{
string strName;
public:
Base& operator=(const Base &b)
{
this->strName = b.strName;
cout << "copy assignment" << endl;
return *this;
}
Base& operator=(string& str)
{
this->strName = str;
cout << "operator=(string& str)" << endl;
return *this;
}
};
class Derive : public Base
{
public:
int num;
using Base::operator =; // unhide Base::operator=();
};
int main(int argc, char *argv[])
{
Derive derive1;
derive1.num = 1;
Derive derive2;
Base b1;
derive1 = b1; // This will call Base& Base::operator=(const Base &b)
//no problem
string str("test");
derive1 = str; // This will call Base& Base::operator=(string& str)
// no problem
derive2 = derive1; // What function will this statement call???
// If it calls Base& Base::operator(const Base &b)
// how could it be assigend to a class Derive?
return 0;
}
But the result of the code is: derive2.num equals to 1!!!, that means the whole class has been copied after the statment, why would this happen?
Thanks to Tony, I think I got the answer.
here is my explanation:
Based on C++0x 7.3.3.3 and 12.8.10, The using-statement in Derive
will be explained like this
class Derive : public Base
{
public:
int num;
//using Base::operator =;
Base& operator=(const Base &b); // comes form the using-statement
Base& operator=(string& str); // comes form the using-statement
Derive& operator=(const Derive &); // implicitly declared by complier
};
So when I wrote:
string str("test");
derive1 = str;
function Base& Base::operator=(string& str);
will be called,
and when I wrote:
Base b1;
derive1 = b1;
function Base& Base::operator=(const Base &b);
will be called,
finnaly, when I wrote:
derive2 = derive1;
function Derive& Dervie::operator=(const Derive&);
will be called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标准 7.3.3-4(来自旧草案,但在这方面仍然有效):
因此,使用隐式
Derived::operator=()
。Standard 7.3.3-4 (from an old draft, but in this regard still valid):
So, the implicit
Derived::operator=()
is used.它将调用派生的
operator=
,它在自动生成的实现中将从Base
调用operator=
并复制中的成员>推导
。It will call the derived
operator=
, which in its automatically generated implementation will calloperator=
fromBase
as well as copy the members inDerive
.