简体   繁体   English

如何在另一个类中创建一个类的参数化构造函数作为数据成员?

[英]How to create parameterized constructor of a class in another class as a data member?


In below example, 在下面的示例中,

class Car
{
        private:
                int sides;

        public:
                Car()
                {
                        cout<<"\ndefault called constructor";
                }
                Car(int nsides)
                {
                        cout<<"\ncalled constructor";
                        sides=nsides;
                }

};

class Auto
{
        private:
                Car ch;
        public:
        Auto(int a) : Car(a)
        {

                //Car test(5);
        }
};

int main()
{
        Auto at(5);
        return 0;

}

After referring below links :- 参考以下链接后:-

create objects in object passing variables through constructor 在通过构造函数传递变量的对象中创建对象

http://www.cplusplus.com/forum/beginner/9746/ http://www.cplusplus.com/forum/beginner/9746/

I tried to write the same and execute it.unfortunately I am getting following compiler error :- 我试图编写相同的代码并执行它。不幸的是,我收到以下编译器错误:-

check.cpp: In constructor ‘Auto::Auto(int)’:
check.cpp:44: error: type ‘Car’ is not a direct base of ‘Auto’

If solution mentioned in the given links are correct then what wrong in my code ? 如果给定链接中提到的解决方案是正确的,那么我的代码有什么问题呢? My next query is ...why only parametrized constructor() throws compiler if try to initialize it without using initialization list. 我的下一个查询是...如果尝试不使用初始化列表进行初始化,那么只有参数化的builder()会抛出编译器。
This will throw compiler error :- 这将引发编译器错误:

class Auto
{
        private:
                Car ch(5);
        public:
        Auto(int a)
        {

        }
};

But this does not :- 但这不是:-

class Auto
{
        private:
                Car ch;
        public:
        Auto(int a)
        {

        }
};

Please help me in understanding this behaviour. 请帮助我理解这种行为。
Thanks in advance. 提前致谢。

In your example you are specifying by your constructor Auto(int a) : Car(a) that Auto is derived from Car, and that's what the compiler complains about. 在您的示例中,您由构造函数Auto(int a) : Car(a) Auto是从Car派生的,而这正是编译器所抱怨的。

To initialize your Car object (inside of Auto), do this Auto(int a) : ch(a) . 要初始化Car对象(在Auto内部),请执行以下Auto(int a) : ch(a) You put the type instead of the member's name. 您输入类型而不是成员的姓名。

About your second question, in-class member initialization is a new feature brought by C++11. 关于第二个问题, 类内成员初始化是C ++ 11带来的新功能。 You may use it by adding the parameter -std=c++11 to your compiler (GCC or Clang, msvc doesn't support it). 您可以通过在编译器中添加参数-std=c++11来使用它(GCC或Clang,msvc不支持它)。 See this question . 看到这个问题 In your case you can use it as chris pointed out : 克里斯指出,您可以使用它:

class Auto {
// ...
Car ch{5};
int someVal = 5;
// ...
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过类构造函数创建成员对象? - How to create member objects by class constructor? 如何在 C++ 的类的默认构造函数中调用成员 object 变量的参数化构造函数? - How to call parameterized constructor of member object variable in a class' default constructor in C++? 如果一个类成员来自另一个类,如何完成构造函数? - How to complete the constructor if a class member comes from another class? 如何在另一个类构造函数中使用一个类的成员 - How to use member of one class in another class constructor 您如何将结构作为 class 中的私有成员并为其创建参数化构造函数/设置器 function? - How do you have a struct as a private member in a class and creating a parameterized constructor/setter function for it? 包含另一个类作为成员变量的类的构造函数 - constructor of the class containing another class as member variables 如何使用构造函数在另一个类中创建对象? - How to create an object inside another class with a constructor? 如何创建在构造函数期间初始化的类成员对象 - How to create class member objects initialized during the constructor 如何在其他类中使用参数化构造函数创建类的对象? - How to make object of a class with parameterized constructor in other class? 如何将对象的动态数组传递给另一个类的参数化构造函数? - How do I pass a dynamic array of objects to a parameterized constructor of another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM