C++ 我们可以根据引用参数类型重载构造函数吗?
我知道我们不能像下面那样重载构造函数(唯一的区别是初始化列表): i、 e.根据不同的参数类型C++ 我们可以根据引用参数类型重载构造函数吗?,c++,constructor,constructor-overloading,C++,Constructor,Constructor Overloading,我知道我们不能像下面那样重载构造函数(唯一的区别是初始化列表): i、 e.根据不同的参数类型一个作为参考类型另一个作为正常类型 我指的是我的老问题: 这能实现吗 我的代码是: #include <iostream> #include <string> using namespace std; class INT{ int i; public: INT(int i=0):i(i){ cout << "INT Class Obje
一个作为参考类型
另一个作为正常类型
我指的是我的老问题:
这能实现吗
我的代码是:
#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;
}
如何强制转换构造函数,使其能够区分重载构造函数的两个版本?您无法回避这样一个事实:不能有两个函数(构造函数或其他函数)使用相同的参数。如果这样做,就不是重载,而是复制函数。编译器不会对按引用而不是按值传递进行特殊异常
作为解决方法,您可以向重载构造函数中添加一个额外的未使用参数(如bool)。假设您有两个函数:
void foo(int const x) {}
void foo(int const& x) {}
当你打电话的时候
foo(10);
编译器无法在两个重载函数之间消除歧义
如果您有:
void foo(int& x) {}
void foo(int const& x) {}
并进行相同的调用,foo(10)代码>,编译器将正确地选择第二个
你有什么
myClass(const INT& oInt,const STRING& oStr);
myClass(const INT oInt,const STRING oStr);
类似于第一对函数
您应该问自己的问题是,导致您创建此类重载的需求是什么。它们是真正的需求吗?除非你的要求与众不同,您应该只能使用其中一个进行管理。您可以在T const&
和T
上重载构造函数,但编译器不能选择导致一对未调用构造函数的构造函数:对于任何可行参数,这两个构造函数都不比另一个好。此外,您不能显式地选择构造函数:构造函数不是普通函数,您不能强制转换它们。因此,在T const&
和T
上重载构造函数是完全无用的。但是,您可以基于右值重载:让一个构造函数采用T常量&
,另一个采用T&
也许你应该问一下你想要实现什么,而不是你如何尝试实现它…好吧,打电话给你
myClass(const STRING& oStr, const INT& oInt):I(oInt),STR(oStr) {}
您可以执行以下任一操作
INT iObj(5);
STRING strObj("Alina Peterson");
INT& x = iObj;
myClass myObj(x, strObj);
或
对于另一个构造函数,我不认为您可以调用它,它总是会导致错误编译器说不!;)也许你可以使用typedefs,但我对此表示怀疑。typedefs对重载分辨率没有任何影响。你想实现什么?如果要启用C++11“移动”而不是复制,请使用右值引用(Type&&
)和std::forward
)。否则,我看不出将引用和值传递给构造函数有什么区别,但可能我还不够努力;myClass(常量INT point,常量STRING oStr)代码>显然参数的类型不同..请参考我在探索构造函数和初始化列表。于是我想到了这样一个想法。我无法解决自己的问题,所以来到这里问
myClass(const INT& oInt,const STRING& oStr);
myClass(const INT oInt,const STRING oStr);
myClass(const STRING& oStr, const INT& oInt):I(oInt),STR(oStr) {}
INT iObj(5);
STRING strObj("Alina Peterson");
INT& x = iObj;
myClass myObj(x, strObj);
myClass myObj(*(&iObj), strObj);