当存在具有默认参数的构造函数时,c ++构造函数中的模糊性 [英] Ambiguity in c++ constructor when a constructor with default argument exists

查看:147
本文介绍了当存在具有默认参数的构造函数时,c ++构造函数中的模糊性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 #include<iostream>
 using namespace std;
 class abc{
    int a;
   public: abc() { } //do nothing constructor
           abc(int x=6){ a=x;} //constructor with default argument  
  };
 main()
  {
      abc a;
    ....
  }

我的问题是将调用哪个构造函数在这种情况下 ?请解释

my question is which constructor will be invoked in this case ? Please explain

推荐答案

这不会编译由于模糊,你可以看到此处

This will not compile due to the ambiguity as you can see here

prog.cpp:8:7: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
prog.cpp: In function ‘int main()’:
prog.cpp:10:11: error: call of overloaded ‘abc()’ is ambiguous
prog.cpp:10:11: note: candidates are:
prog.cpp:6:12: note: abc::abc(int)
prog.cpp:5:12: note: abc::abc()

这篇关于当存在具有默认参数的构造函数时,c ++构造函数中的模糊性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆