Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6601001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:41:48+00:00 2026-05-25T18:41:48+00:00

I have a specific situation where I’ve got an object that I want to

  • 0

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.

First, my header:

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
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:41:49+00:00Added an answer on May 25, 2026 at 6:41 pm

    What you want is an initializer list. For example:

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

    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) {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a specific changeset that I want to rollback my Development branch to,
I have a specific question, that could use a general answer... When building Multi-Tier
I have a specific situation with slideToggle that creates a flicker in Firefox. The
By 'situation specific' I mean it uses some data that it would have access
So situation is following : We have some very old product, that has dozen
I have a problem with a specific java client library. Here is the situation:
I have a specific requirement that all children of a particular JComponent have double
I have some specific id like 1,2,5,11,64589 in solr (int type) I want to
I have a specific Silverlight application, that is fed with data by a WCF-Service.
The only SET parameter that I have found specific guidance for is SET TRANSACTION

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.