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

  • SEARCH
  • Home
  • 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 7089893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:58:40+00:00 2026-05-28T07:58:40+00:00

Previously I had: MyClass::MyClass() : myMember(VALUE,this,5,etc.) { … } but I need to do

  • 0

Previously I had:

MyClass::MyClass() : myMember(VALUE,this,5,"etc.") { ... }

but I need to do some calculation of the values passed to myMember’s constructor. How do I initialise myMember within the constructor body instead? I’m not sure if I should do it like this:

MyMember myMember(calculatedValue,this,5,"etc.");
  • 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-28T07:58:41+00:00Added an answer on May 28, 2026 at 7:58 am

    All member initialisation occurs before the constructor body runs. So if you did this:

    MyClass::MyClass()
    {
        myMember = MyMember(calculatedValue,this,5,"etc.");
    }
    

    it would be assignment, not initialisation. So it won’t work if MyMembers are not assignable (e.g. they have a private assignment operator). It also won’t work if myMember is declared as const.

    You could, however, do something like this:

    class MyClass {
    public:
        MyClass() : myMember(calculate(VALUE),this,5,"etc.") {}
    
    private:
        static int calculate(int x) { return x*3; }
    };
    

    or even something like this:

    class MyClass {
    public:
        MyClass() : myMember(createMember()) {}
    
    private:
        static MyMember createMember(int x) {
            MyMember m;
            m.foo = 5;
            ...
            return m;
        }
    };
    

    Note that I’ve used static helper functions here, because using non-static member functions before the constructor has completed gets a little weird, in the general case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I actually previously had a much longer question but I figured this would be
I previously had a post on this issue that was resolved. However since rebuilding
Previously we had desktop applications but given the fact that accessing the server (either
I have tried everything and this makes absolutely no sense! I previously had Less.js
I have a class that contains a list of objects. I previously had this
I previously had this problem with an external file not running. I fixed this
I previously had Passenger 2.2.13 install with NGINX 0.7.65 - I want to install
I previously had a table that is created using hibernate entities. I am using
I have a follow-up question regarding an issue I previously had on SO here
Here is the site in question: http://jayduff.co.uk/ I previously had the images/links on the

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.