gpt4 book ai didi

c++ - 我们可以根据引用参数类型重载构造函数吗?

转载 作者:行者123 更新时间:2023-11-28 02:49:01 24 4
gpt4 key购买 nike

我知道我们不能像下面这样重载构造函数(唯一的区别是初始化列表):

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);

即基于不同的参数类型 一个作为引用类型 另一个作为普通类型

我指的是我的老问题: 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){
^

我如何转换我的构造函数,以便它可以区分重载构造函数的两个版本?

最佳答案

您可以在 T const&T 上重载构造函数,但编译器无法选择构造函数,从而导致一对无法调用的构造函数:没有一个比另一个更好对于任何可行的论点。此外,您不能显式选择构造函数:构造函数不是普通函数,您不能强制转换它们。因此,在 T const&T 上重载构造函数是完全没有用的。但是,您可以基于右值重载:让一个构造函数采用 T const&,另一个构造函数采用 T&&

也许你应该问你想实现什么,而不是你如何尝试实现它...

关于c++ - 我们可以根据引用参数类型重载构造函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23476742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com