使用C ++中的非默认构造函数初始化对象的成员类 [英] Initializing a member class of an object using a non-default constructor in C++

查看:254
本文介绍了使用C ++中的非默认构造函数初始化对象的成员类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个特定的情况,我有一个对象,我想使用boost随机数生成器,它导致一个更大的问题,我似乎无法回答。这是我想要产生的示例代码。

I have a specific situation where I've got an object that I want to use the boost random number generators on, and it has lead to a greater question which I cannot seem to answer. Here is the example code of what I'm trying to produce.

首先,我的标题:

Class MyObject {

 protected:
    double some variable;
    boost::random::mt19937 rgenerator;
    boost::uniform_real<double> dist_0_1;
    boost::variate_generator< boost::mt19937&, boost::uniform_real<double> > rand01
}



现在我想做的是:

Now what I want to do is:

Class MyObject {

 protected:
    double some variable;

    boost::random::mt19937 rgenerator(std::time(0)); //initialize to a "random" seed
    boost::uniform_real<double> dist_0_1(0,1); //set the distribution to 0-1
    boost::variate_generator< boost::mt19937&, boost::uniform_real<double> > rand01(rgenerator, dist_0_1);//tell it to use the above two objects
}

但是这不工作,因为它在一个标题。我想我可以使用MyObject的构造函数调用各种子对象的构造函数(分布,生成器,但我不知道如何。当调用MyObject的构造函数时,子对象的默认构造函数已经被调用,我还没有发现他们有成员方法来重置这些属性...除此之外,这不是我的困惑点,现在也许有太多的事情,我可以告诉,我的问题减少到以下,幼稚的例子:

But this doesn't work because it is in a header. I thought I could use the constructor of MyObject to somehow call the constructors on the various sub-objects (distribution, generator, but I can't figure out how. By the time the constructor of MyObject is called, the sub-objects' default constructors have already been called, and I haven't found that they have member methods to reset these properties... besides which, that isn't the point where I am confused. Now maybe there are too many things going on and I'm confusing issues, but as far as I can tell, my problem reduces to this following, childish example:

Class Tree {

    Tree();
    Tree(int);

    protected: 

        fruit apples(int);
}

Tree::Tree() {
    apples(0); //won't work because we can't call the constructor again?
}

Tree::Tree(int fruit_num) {
    apples(fruit_num); //won't work because we can't call the constructor again?
}

Class Fruit {

    public:
        Fruit();
        Fruit(int);

    protected:
        int number_of_fruit;

}

Fruit::Fruit() {

    number_of_fruit = 0;
}

Fruit::Fruit(int number) {

    number_of_fruit = number;

}

我相信这对其他人来说是第二个性质但是我找不到一篇介绍将对象的成员对象初始化为非默认构造函数值的最佳实践的文章。

I'm sure this is second nature to everyone else out there, but I can't find an article that talks about the best practice for initializing member objects of an object to a non-default constructor value.

推荐答案

你想要的是一个初始化列表。例如:

What you want is an initializer list. For example:

Tree::Tree(int fruit_num) 
    : apples(fruit_num) // Initializes "apple" with "fruit_num"
{
}

code>:)后面的构造函数参数和开始大括号 {。您可以使用逗号()分隔不同的成员构造函数。示例:

You simply add a colon (:) after the constructor parameters and before the opening brace {. You can separate different member constructors with commas (,). Example:

Tree::Tree(int fruit1, int fruit2) : apples(fruit1), bananas(fruit2) {
}

这篇关于使用C ++中的非默认构造函数初始化对象的成员类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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