C++ 构造函数调用问题

C++ 构造函数调用问题,c++,c++11,c++14,c++builder,C++,C++11,C++14,C++builder,在这段代码尝试创建dobj4时得到了编译器错误 #include<iostream> using namespace std; class mod; class name { friend mod; public: name(const char* n) { cout << "1 arg constructor for name\n"; } n

在这段代码尝试创建dobj4时得到了编译器错误

#include<iostream>
using namespace std;
class mod;
class  name {
    friend mod;
    public:
            name(const char* n) {
                    cout << "1 arg constructor for name\n";
            }
            name() {
                    cout << "no arg constructor for name\n";
            }
            name(const char* n, int i ){
                    cout << "2 arg constructor for name\n";
            }
      };

class mod {
    public:
    mod() {
            cout << "base class constructor invoked\n";
    }
};

struct derived : mod {

    derived(name) {
            cout << "derived constructor invoked\n";
    }
 };

int main() {
    name nobj;
    derived dobj(nobj);

    name nobj1("hello");
    derived dobj1(nobj1);

    derived dobj2("Hi");

    name nobj2("yo", 2);
    derived dobj3(nobj2);

 //      derived dobj4("go", 4);

    return 0;
}
#包括
使用名称空间std;
类mod;
类名{
朋友mod;
公众:
名称(常量字符*n){

cout我觉得您的问题可以有更简短、更容易理解的示例代码:(我还修复了使其可编译)

我留下了派生名称,即使继承在这里没有相关性,我已经删除了它,因为它不重要

#include<iostream>
class  name {
public:
    name() {
        std::cout << "no arg constructor for name\n";
    }
    name(const char* n) {
        std::cout << "1 arg(" << n <<") constructor for name\n";
    }
    name(const char* n, int i ) {
        std::cout << "2 arg(" << n << ',' << i <<") constructor for name\n";
    }
};
struct derived {

    derived(const name& n ) {
        std::cout << "derived name constructor invoked\n";
        (void)n;
    }
};

int main() {

    name nobj;
    derived dobj(nobj);

    derived dobj2("Hi");

    derived dobj4({"go", 4}); //see the initialization list instead of 2 parameters passing

    return 0;
}
#包括
类名{
公众:
姓名(){

std::cout我觉得您的问题可以有更简短、更容易理解的示例代码:(我还修复了使其可编译)

我留下了派生名称,即使继承在这里没有相关性,我已经删除了它,因为它不重要

#include<iostream>
class  name {
public:
    name() {
        std::cout << "no arg constructor for name\n";
    }
    name(const char* n) {
        std::cout << "1 arg(" << n <<") constructor for name\n";
    }
    name(const char* n, int i ) {
        std::cout << "2 arg(" << n << ',' << i <<") constructor for name\n";
    }
};
struct derived {

    derived(const name& n ) {
        std::cout << "derived name constructor invoked\n";
        (void)n;
    }
};

int main() {

    name nobj;
    derived dobj(nobj);

    derived dobj2("Hi");

    derived dobj4({"go", 4}); //see the initialization list instead of 2 parameters passing

    return 0;
}
#包括
类名{
公众:
姓名(){

std::cout转换构造函数的规则在
C++03
C++11
中有所不同

C++03
中:只有一个参数的构造函数,或者如果有多个参数,则具有默认值的其余参数的构造函数是隐式可转换的

例如:

name(const char* n) {}
name(int n, int i = 0) {} // i has a default value
derived dobj4({"go", 4}); // call to name(const char* n, int i) is brace-initialized
C++11
中:上面
C++03
中定义的所有情况以及具有多个参数的构造函数。但此类构造函数调用需要初始化

例如:

name(const char* n) {}
name(int n, int i = 0) {} // i has a default value
derived dobj4({"go", 4}); // call to name(const char* n, int i) is brace-initialized

不用说,如果构造函数被声明为显式的,它将不会被转换。

转换构造函数的规则在
C++03
C++11
中有所不同

C++03
中:只有一个参数的构造函数,或者如果有多个参数,则具有默认值的其余参数的构造函数是隐式可转换的

例如:

name(const char* n) {}
name(int n, int i = 0) {} // i has a default value
derived dobj4({"go", 4}); // call to name(const char* n, int i) is brace-initialized
C++11
中:上面
C++03
中定义的所有情况以及具有多个参数的构造函数。但此类构造函数调用需要初始化

例如:

name(const char* n) {}
name(int n, int i = 0) {} // i has a default value
derived dobj4({"go", 4}); // call to name(const char* n, int i) is brace-initialized


不用说,如果构造函数声明为显式
,它将不会转换。

构造函数不会自动继承。派生的构造函数没有接受两个参数的构造函数。请在派生为派生(const char*n,int n){//impression}中添加一个构造函数它应该是可行的。我怀疑你误解了参数的隐式转换是如何工作的。在你最喜欢的C++书籍中读一些关于它的内容。是的,C++的新的,我正在尝试理解DCOB2是如何发生的,而不是在DOb4中,谢谢。support@ashkun我认为你需要澄清你的问题。理解这个问题花了一些时间即使来自Molbdniloconstructor的注释也不会自动继承您的问题。派生的构造函数没有接受两个参数的构造函数。请在派生为派生(const char*n,int n){//impression}中添加构造函数它应该是可行的。我怀疑你误解了参数的隐式转换是如何工作的。在你最喜欢的C++书籍中读一些关于它的内容。是的,C++的新的,我正在尝试理解DCOB2是如何发生的,而不是在DOb4中,谢谢。support@ashkun我认为你需要澄清你的问题。理解这个问题花了一些时间即使是molbdniloI的评论,你的问题也在于你的意思是说构造函数是一个“转换构造函数”如果它满足这些参数条件。但这与问题无关,只是Terminology是的,其中一个没有默认参数。仍然不确定您想说什么。对于C++11标志,应该使用大括号初始化来构造它,即使有多个参数没有默认值e除了第一行。我的示例中的第一行编译(使用
({})
),第二行仅使用
{}
不编译。哦,我明白了。但是上面代码中的默认参数使构造函数可能不明确。这分散了我的注意力。啊。我的示例不好。纠正了它。我想你的意思是说构造函数是一个“转换构造函数"如果它满足这些参数条件。但这与问题无关,只是Terminology是的,其中一个没有默认参数。仍然不确定您想说什么。对于C++11标志,应该使用大括号初始化来构造它,即使有多个参数没有默认值e除了第一行。我的示例中的第一行编译(使用
({})
),第二行仅使用
{}
不编译。哦,我明白了。但是上面代码中的默认参数使构造函数可能不明确。这分散了我的注意力。啊。我的示例不好。纠正了它。