3

I am compiling a class, the complete program to which is given below:

#include<iostream>
using namespace std;

class Test{
    public:
        Test()
        {
            cout<<"Test variable created...\n";
            // accessing width variable in constructor
            cout<<"Width is "<<width<<".\n";
        }
        void setHeight(int h)
        {
            height = h;
        }
        void printHeight()
        {
            cout<<"Height is "<<height<<" meters.\n";
        }
        int width = 6;
    protected:
        int height;
};

int main()
{
    Test t = Test();
    t.setHeight(3);
    t.printHeight();
    return 0;
}

The code works absolutely fine, but how is the constructor able to access the variable width which has not been declared until the end of public block. Also, how are the member functions able to access the variables declared later in the public block? Isn't C++ sequential (executing the statements in the order they are written)?

6
  • 2
    Declarations are not statements. A class's declarations are collected and stored before trying to compile its code. Also, statements needn't be executed sequentially iff the observable behaviour of the program would be the same, AFAIK, under the 'as-if' rule. Jan 28, 2021 at 16:08
  • "Isn't C++ sequential (executing the statements in the order they are written)?" No, it's not. Statements can be executed in different order, e.g. member initializer lists. The statements are executed in the order the members are listed in the class and not in the order of the actual statements. Jan 28, 2021 at 16:08
  • Declare as int height{}; so that users can't invoke undefined behaviour by trying to printHeight() without having setHeight(). Jan 28, 2021 at 16:10
  • 2
    Member variables are initialized (int width = 6;) before the constructor is called. That means that the structure of the class has to be known before the first non-static method can be called. Jan 28, 2021 at 16:13
  • Here you can see what happens before the actual constructor starts its work. There are better documentations for this but I wasn't able to find them. Jan 28, 2021 at 16:19

2 Answers 2

4

Think of inline definitions in a class as just syntactic sugar for declaring the function, and then defining outside of the class. Manually doing that would transform the code to

class Test{
    public:
        Test();
        void setHeight(int h);
        void printHeight();
        int width = 6;
    protected:
        int height;
};

Test::Test()
{
    cout<<"Test variable created...\n";
    // accessing width variable in constructor
    cout<<"Width is "<<width<<".\n";
}

void Test::setHeight(int h)
{
    height = h;
}

void Test::printHeight()
{
    cout<<"Height is "<<height<<" meters.\n";
}

And you can see from this transformation that the class member is now "before" the function definitions, so there is no reason why they can't know about the variable.

The technical term for this is call the complete-class context and the jist of it is that when you are in the body of a member function or the class member initialization list, the class is considered complete and can use anything defined in the class, no matter where in the class it is declared.

4
  • Is it also true in the initializer list of a constructor ? Jan 28, 2021 at 16:26
  • 1
    @VincentFourmond Yes. The member initialization list is consider part of the function body Jan 28, 2021 at 16:28
  • Thanks @NathanOlivier I guess it would be a valuable precision in your answer ? Jan 28, 2021 at 16:35
  • @VincentFourmond added. Jan 28, 2021 at 16:36
0

This is because the body of a member function is a complete-class context of a class, as mentioned in the quoted statements below:

From class.mem.general#6:

6. A complete-class context of a class is a:

  • function body ([dcl.fct.def.general]),

  • default argument,

  • noexcept-specifier ([except.spec]), or

  • default member initializer

within the member-specification of the class.

The above means that a function body is a complete-class context of a class which in turn means that the usage of width inside the constructor and the usage of height inside member function setHeight and printHeight is allowed here even though those data members appear later when writing the class definition.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.