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 7988779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:33:24+00:00 2026-06-04T12:33:24+00:00

Consider the following (simplified) situation: class Foo { private: int evenA; int evenB; int

  • 0

Consider the following (simplified) situation:

class Foo
{
private:
    int evenA;
    int evenB;
    int evenSum;
public:
    Foo(int a, int b) : evenA(a-(a%2)), evenB(b-(b%2)), evenSum(evenA+evenB)
    {
    }
};

When i instanciate Foo like this:

Foo foo(1,3);

then evenA is 0, evenB is 2, but will evenSum be initialized to 2?

I tried this on my current platform (iOS) and it seems to work, but I’m not sure whether this code is portable.

Thanks for your help!

  • 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-06-04T12:33:26+00:00Added an answer on June 4, 2026 at 12:33 pm

    This is well-defined and portable,1 but it’s potentially error-prone.

    Members are initialized in the order they’re declared in the class body, not the order they’re listed in the initialization list. So if you change the class body, this code may silently fail (although many compilers will spot this and emit a warning).


    1. From [class.base.init] in the C++ standard(s):

    In a non-delegating constructor, initialization proceeds in the following order:

    • First, and only for the constructor of the most derived class (1.8), virtual base classes are initialized in
      the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes,
      where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
    • Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list
      (regardless of the order of the mem-initializers).
    • Then, non-static data members are initialized in the order they were declared in the class definition
      (again regardless of the order of the mem-initializers).
    • Finally, the compound-statement of the constructor body is executed.

    (Highlighting is mine.)

    This section of the standard then goes on to give an example of using member variables to initialize other member variables.

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

Sidebar

Related Questions

Consider the following simplified objects and relationships: public class Job{ int JobId; String name;
consider the following simplified example: public class Ticket { public int Id; public TicketState
Let's consider the following simplified Resource hierarchy: public abstract class Resource { static public
Consider the (highly simplified) following case: class Dispatcher { public: receive() {/*implementation*/}; // callback
Consider the following: object Main { case class Foo(bar: Int) extends FooList { val
Consider the following class (simplified in order to focus in the core problem): public
Consider the following simplified demonstration: Class X contain Class Y . Class Y has
Consider the following simplified version of my code. I have a template class A
Consider the following (simplified) example: abstract class Bar[T] { val f: PartialFunction[T, T] val
Consider following simple DAO example: public abstract class DAOFactory { public abstract AccountDAO getAccountDAO();

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.