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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:00:41+00:00 2026-06-12T20:00:41+00:00

#include <iostream> using namespace std; class Ex { private: int i; float f; public:

  • 0
#include <iostream>
using namespace std;
class Ex {
private:
    int i;
    float f;
public:
    Ex(int i,float f):i(i),f(f) {
        cout << this->i << '\t' << this->f << endl;
    }
    ~Ex(){
        cout << "destructor";
    }
};
int main() {
    Ex i(10,20.1f);
}

In the program above I wrote above,if the constructor was parameterized constructor like the following:

Ex(int i,float f){
        i=i;
        f=f;
        cout << this->i << '\t' << this->f << endl;
    }

here the data members of the object are initialized to junk because data members are hidden due to local variables of same name.
But in the program above it works fine without explicit this.How?

  • 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-12T20:00:42+00:00Added an answer on June 12, 2026 at 8:00 pm

    Similar questions have been asked before, e.g. here and here, but while many answers point out that this is (contrary to what Konstantin D – Infragistics says) not compiler-specific, I couldn’t find any answer that actually quotes the relevant parts of the Standard.

    So here they are. I’ve added emphasis to highlight the key statements.

    (§12.6.2/10) 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.

    (§12.6.2/12) Names in the expression-list or braced-init-list of a mem-initializer are evaluated in the scope of the constructor for which the mem-initializer is specified. [ Example:

        class X {
          int a;
          int b;
          int i;
          int j;
        public:
          const int& r;
          X(int i): r(a), b(i), i(i), j(this->i) { }
        };
    

    initializes X::r to refer to X::a, initializes X::b with the value of the constructor parameter i, initializes X::i with the value of the constructor parameter i, and initializes X::j with the value of X::i; this takes place each time an object of class X is created. — end example ]
    [ Note: Because the mem-initializer are evaluated in the scope of the constructor, the this pointer can be used in the expression-list of a mem-initializer to refer to the object being initialized. — end note ]

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

Sidebar

Related Questions

Consider this code: #include <iostream> using namespace std; class hello{ public: void f(){ cout<<f<<endl;
#include<iostream> using namespace std; class Something { public: int j; Something():j(20) {cout<<Something initialized. j=<<j<<endl;}
#include <iostream> using namespace std; class A{ int b; public: A(){ cout<<Constructor for class
#include<iostream> using namespace std; class Abc { public: int a; Abc() { cout<<Def cstr
#include<iostream> #include<stdlib.h> #includeheader_complex.h using namespace std; class Complex { private: float real,imag; public: Complex();
#include<iostream> using namespace std; class A { private: int value; public: A(int init):value(init){} void
#include <iostream> using namespace std; class Fraction { private: int num; int denom; public:
#include <iostream> using namespace std; struct testarray{ int element; public: testarray(int a):element(a){} }; class
#include <iostream> using namespace std; class A { public: A() { cout << Default
Look at this code please: #include <iostream> using namespace std; class Ratio { public:

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.