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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:37:05+00:00 2026-05-30T14:37:05+00:00

Possible Duplicate: Constructor initialization-list evaluation order While writing a C++ constructor for a class,

  • 0

Possible Duplicate:
Constructor initialization-list evaluation order

While writing a C++ constructor for a class, why is it that the order of initialising the member fields should be the order in which they are declared?

Consider the following C++ code. On compiling with gcc (gcc -g -Wall foo.cpp) I get the Warning

g++ -g -Wall main.cpp 
main.cpp: In constructor ‘myclass::myclass(int, int, int, int, int, int)’:
main.cpp:12: warning: ‘myclass::z’ will be initialized after
main.cpp:11: warning:   ‘int myclass::y’
main.cpp:26: warning:   when initialized here

Here is the code. In this, the member z appears in the initialization list of the
constructor class before y and throws the above warning.

#include <iostream>
#include <iomanip>

class myclass
{
public:
  int x;
  int y;
  int z;
  myclass(int num1, int num2, int num3, int num4, int num5, int num6);//constructor for the class

private:
  int a;
  int b;
  int c;
};

myclass::myclass(int num1, int num2, int num3, int num4, int num5, int num6) 
  :x(num1),z(num3),  y(num2), a(num4),b(num5),c(num6)
{}

int main(int argc, char *argv[])
{
  myclass jimmy(23,34,56,67,89,91);

  std::cout << jimmy.x << std::endl;
  std::cout << jimmy.y << std::endl;
  std::cout << jimmy.z << std::endl;

  return 0;
}
  • 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-30T14:37:07+00:00Added an answer on May 30, 2026 at 2:37 pm

    This may help,

    Constructor initialization-list evaluation order

    Please see AProgrammer’s reply in the above,

    “The reason for which they are constructed in the member declaration order and not in the order in the constructor is that one may have several constructors, but there is only one destructor. And the destructor destroy the members in the reserse order of construction.” – AProgrammer Aug 7 ’09 at 6:45

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

Sidebar

Related Questions

Possible Duplicates: Why should I prefer to use member initialization list? C++ - what
Possible Duplicate: Initialize class fields in constructor or at declaration? We are arguing about
Possible Duplicate: C# constructor execution order class Foo { public int abc; Foo() {
Possible Duplicate: Best Practice: Initialize class fields in constructor or at declaration? I am
Possible Duplicate: Why XML-Serializable class need a parameterless constructor Does anyone know if it
Possible Duplicate: What does a colon following a C++ constructor name do? Class construction
Possible Duplicate: Why can’t I create an abstract constructor on an abstract C# class?
Possible Duplicate: “Undefined reference to” template class constructor I've just started using templates, and
Possible Duplicate: Why can’t I create an abstract constructor on an abstract C# class?
Possible Duplicate: Should I initialize variable within constructor or outside constructor I have here

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.