2

This some code I wrote:

#include <iostream>

class Number
{
    int n;
public:
    Number(int n)
    {
        std::cout << "Constructed object: " << n << std::endl;
        this->n = n;
    }
};

Number b = 2; //when does this ctor get called?

int main()
{
    Number a = 4;
}

Output:

Constructed object: 2
Constructed object: 4

When does the constructor of the global object get called? Right before main is executed?

DarkAtom
  • 2,589
  • 1
  • 11
  • 27

2 Answers2

3

When does the constructor of the global object get called? Right before main is executed?

Essentially, yes. Within a translation unit, objects are constructed in the order that they appear. Across translation units, the order is undefined.

Objects are destroyed in the opposite order to their construction.

For gcc (and I think, also, clang), see also: How exactly does __attribute__((constructor)) work?

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Looking for a good standard quote, but I recall objects in namespaces can get a bit murky. – user4581301 Sep 16 '19 at 20:27
  • @user4581301 Yes, that would improve the answer (or post an answer of your own). – Paul Sanders Sep 16 '19 at 20:28
  • Digging through [**Basic.start**](https://timsong-cpp.github.io/cppwp/n4659/basic.start) (C++17, or as close as I have a link.) The nasties are zero-initialized and constant-initialized variables (they happen first). After that the order is pretty sane, until you hit `thread-local` variables and `inline` variables which can be initialzed after `main`, but before first use. I don't think I understand it all enough yet to write a n answer and it's going way beyond the case the Asker's asking about. – user4581301 Sep 16 '19 at 22:56
  • @user4581301 Yes, I took a look too. It all makes sense, but I agree, it goes beyond what the OP wanted to know. The main thing I took from it was that variables with no constructor are initialised first (which is what you would expect - they are compile-time constructs), – Paul Sanders Sep 16 '19 at 23:36
  • @user4581301: These are good points. I closed the question as a duplicate, since the exceptions you mention do not apply here. But it might be an option to ask & self-answer a similar question focusing on the cases you named there. – MSalters Sep 17 '19 at 12:09
0

yes, the constructor of the global object gets called before main()

for additional information: here I'm listing when the constructors gets called for different types of object like global, local,static local,dynamic

1)for global object you already wrote a program

2)For a non-static local object, constructor is called when execution reaches point where object is declared

using namespace std; 
class Test 
{ 
public: 
Test(); 
}; 

Test::Test() { 
    cout << "Constructor Called \n"; 
} 

void fun() { 
Test t1; 
} 

int main() { 
    cout << "Before fun() called\n"; 
    fun(); 
    cout << "After fun() called\n"; 
    return 0; 
} 
/* OUTPUT: 
    Before fun() called 
    Constructor Called 
    After fun() called 
*/

For a local static object, the first time (and only the first time) execution reaches point where object is declared. 3)Class Scope: When an object is created, compiler makes sure that constructors for all of its subobjects (its member and inherited objects) are called. If members have default constructurs or constructor without parameter then these constrctors are called automatically, otherwise parameterized constructors can be called using Initializer List.

// PROGRAM 1: Constrcuctor without any parameter 
#include<iostream> 
using namespace std; 

class A 
{ 
public: 
A(); 
}; 

A::A() { 
    cout << "A's Constructor Called \n"; 
} 

class B 
{ 
A t1; 
public: 
B(); 
}; 

B::B() { 
    cout << "B's Constructor Called \n"; 
} 

int main() { 
    B b; 
    return 0; 
} 
/* OUTPUT: 
    A's Constructor Called 
    B's Constructor Called 
*/
mudassir ahmed
  • 191
  • 1
  • 1
  • 13