0

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

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

我指的是我的老问题: 重载类成员函数

可以实现吗?

我的代码是:

#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){
 ^

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

4

4 回答 4

2

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

也许您应该询问想要实现的目标,而不是您如何尝试实现它...

于 2014-05-05T16:24:36.723 回答
1

假设您有两个功能:

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

类似于第一对函数。

您应该问自己的问题是导致您创建此类重载的要求是什么。它们是真正的需求吗?除非您的要求是不寻常的,否则您应该能够仅使用其中一个来进行管理。

于 2014-05-05T16:19:18.257 回答
0

好打电话

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

对于另一个构造函数,我认为你不能调用它,总是会导致错误

于 2014-05-05T16:48:55.410 回答
-1

您无法绕过这样一个事实,即您不能拥有两个采用相同参数的函数(构造函数或其他函数)。如果你这样做,你并没有重载,你只是在复制函数。对于按引用而不是按值传递,编译器不会产生特殊异常。

作为一种变通方法,您可以在重载的构造函数中添加一个额外的未使用参数(如布尔值)。

于 2014-05-05T16:08:36.210 回答