【发布时间】:2014-05-05 15:55:05
【问题描述】:
我知道我们不能像下面这样重载构造函数(唯一的区别是初始化列表):
myClass(const INT& oInt,const STRING& oStr):I(oInt),STR(oStr){
cout << "my Class Object Created with I : " << I << " STR : " << STR << endl;
}
myClass(const INT& oInt,const STRING& oStr){
I=oInt;
STR=oStr;
cout << "my Class Object Created with I : " << I << " STR : " << STR << endl;
}
但假设我想将我的构造函数重载为:
myClass(const INT& oInt,const STRING& oStr):I(oInt),STR(oStr);
myClass(const INT oInt,const STRING oStr);
即基于不同的参数类型one as reference type其他as normal type
我指的是我的老问题: Overloading Class Member Function
可以实现吗?
我的代码是:
#include <iostream>
#include <string>
using namespace std;
class INT{
int i;
public:
INT(int i=0):i(i){
cout << "INT Class Object Created with i : " << i << endl;
}
~INT()
{
cout << "INT Class Object Destructed with i :" << i << endl;
}
INT(const INT& I){
i=I.i;
cout << "INT Class Copy Constructor called for i : "<< i << endl;
}
INT& operator= (const INT& I){
i=I.i;
cout << "INT Assignment operator called for i : "<< i << endl;
return *this;
}
friend ostream& operator << (ostream& os,const INT& I){
os << I.i ;
return os;
}
};
class STRING{
string str;
public:
STRING(string str=""):str(str){
cout << "STRING Class Object Created with str : " << str << endl;
}
~STRING()
{
cout << "STRING Class Object Destructed with str :" << str << endl;
}
STRING(const STRING& S){
str=S.str;
cout << "STRING Class Copy Constructor called for str : "<< str << endl;
}
STRING& operator= (const STRING& S){
str=S.str;
cout << "STRING Assignment operator called for str : "<< str << endl;
return *this;
}
friend ostream& operator << (ostream& os,const STRING& S){
os << S.str ;
return os;
}
};
class myClass{
INT I;
STRING STR;
public:
myClass(const INT& oInt,const STRING& oStr):I(oInt),STR(oStr){
cout << "my Class Object Created with I : " << I << " STR : " << STR << endl;
}
myClass(const INT oInt,const STRING oStr){
I=oInt;
STR=oStr;
cout << "my Class Object Created with I : " << I << " STR : " << STR << endl;
}
~myClass()
{
cout << "my Class Object Destructed with I : " << I << " STR : " << STR << endl;
}
};
int main()
{
INT iObj(5);
STRING strObj("Alina Peterson");
cout << "\n\n======================================================\n\n" << endl;
myClass myObj(iObj,strObj);
cout << "\n\n======================================================\n\n" << endl;
return 0;
}
我得到的错误是:
$ g++ -g -Wall CPP.cpp -o CPP
CPP.cpp: In function ‘int main()’:
CPP.cpp:116:32: error: call of overloaded ‘myClass(INT&, STRING&)’ is ambiguous
myClass myObj(iObj,strObj);
^
CPP.cpp:116:32: note: candidates are:
CPP.cpp:86:1: note: myClass::myClass(INT, STRING)
myClass(const INT oInt,const STRING oStr){
^
CPP.cpp:81:1: note: myClass::myClass(const INT&, const STRING&)
myClass(const INT& oInt,const STRING& oStr):I(oInt),STR(oStr){
^
如何转换我的构造函数,以便区分两个版本的重载构造函数?
【问题讨论】:
-
编译器说不! ;) 也许你可以使用 typedefs,但我对此表示怀疑。
-
Typedef 对重载解析没有任何影响。
-
你想要完成什么?如果要启用 C++11 “移动”而不是复制,请使用右值引用 (
Type&&) 和std::forward。否则,我看不出区分将引用和值传递给构造函数的任何意义,但也许我还不够努力。
标签: c++ constructor constructor-overloading