【问题标题】:Constructor calling ambiguity in C++ [duplicate]C ++中的构造函数调用歧义[重复]
【发布时间】:2014-01-13 07:28:45
【问题描述】:
#include <string.h>
#include <iostream>
using namespace std;

class String
{
    public:
        /* Parameterized Constructor */
        String(const char* i_ac)
        {
            cout<<"Parameterized Constructor";
            if(i_ac)
            {
                data = new char [strlen(i_ac) + 1];
                strcpy(data,i_ac);
            }
            else
            {
                data = new char[1];
                *data = '\0';
            }
        }
        //Parametrized constructor ends.

        /*Copy Constructor */
        String ( String& objTemp )
        {
            cout<<"Copy Constructor";
            data = new char[strlen(objTemp.data) + 1];
            strcpy(data,objTemp.data); 
        }

        /*Overloaded Assignment Operator */
        String operator=(String& objTemp)
        {
            if(this == &objTemp)
            return objTemp;
            //Delete Existing data
            delete[] data;
            data = new char[strlen(objTemp.data)+ 1];
            strcpy(data,objTemp.data);
            return *this;
        }

        friend ostream& operator<<(ostream& out, String& s1);

    private :
        char* data;
};

ostream& operator<<(ostream& o1, String& s1)
{
    o1<<s1.data;
    return o1;
}

int main()
{
    String s1("Hello");
    cout<<"s1"<<s1;
    String s2 = s1;
    cout<<"\ns2"<<s2;
    String s3(); //doubt
    cout<<"\ns3"<<s3;
}

在调用String s3() 时,不会调用构造函数(我知道,因为我在每个构造函数中打印)。但是,在下一行打印 s3 会输出 1。

为什么不调用构造函数?我怎样才能确保有人调用?

【问题讨论】:

  • 不是错误的原因,但您确实应该传递const String&amp;
  • 字符串 s3();我认为被视为函数声明。这就是为什么不调用构造函数的原因。如果你删除 (),你最终会调用不带参数的构造函数,它不存在。

标签: c++ string c++11 constructor


【解决方案1】:

请注意:试试 clang,它有一些非常好的诊断:

t.cpp:64:10: warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
String s3(); //doubt
         ^~
t.cpp:65:15: warning: address of function 's3' will always evaluate to 'true' [-Wbool-conversion]
cout<<"\ns3"<<s3;
~~~~          ^~
t.cpp:65:15: note: prefix with the address-of operator to silence this warning
cout<<"\ns3"<<s3;
              ^
              &
2 warnings generated.

【讨论】:

    【解决方案2】:

    为什么不调用构造函数?我怎样才能确保有人调用?

    这不行,这不会调用空的ctor,它会创建一个本地方法。

    String s3(); //doubt
    cout<<"\ns3"<<s3;
    

    要完成这项工作,请从 String s3() 中删除 ();成为字符串 s3;

    String s3; // This is the correct form
    

    (我认为这是开始使用 C++ 时最常见的错误之一)。 但请注意,这将触发链接器错误,因为您没有定义空 ctor。我想您也想根据那里的 if/else 条件使用“参数化”ctor 充当空 ctor。为此,您必须为参数 i_ac 提供默认值 0 或 NULL。或者,如果您使用的是 C++11,请使用 nullptr,因为 NULL 被认为是可憎的。

    旁注:

    但是,在下一行打印 s3 会输出 1。

    这会将 s3 评估为布尔值,因此会输出“1”。

    /*Copy Constructor */
    String ( String& objTemp )
    

    这真的应该写成

    /*Copy Constructor */
    String ( const String& objTemp )
    

    同样适用

    /*Overloaded Assignment Operator */
    String operator=(String& objTemp)
    
    // Add const
    String operator=(const String& objTemp)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 2017-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多