extdummy
extdummy

Reputation: 221

Is the C++ default constructor always called, as indicated in C++ Primer?

In C++ primer 4th edition (Stanley Lipmann) on page 52, there is a sentence which says:

The default constructor is used regardless of where a variable is defined.

Can anyone explain a bit more? This statements seem like it is missing something.

Upvotes: 9

Views: 14614

Answers (4)

Chubsdad
Chubsdad

Reputation: 25497

From Little Cambridgeport Design Factory, clause 12:

The default constructor (12.1), copy constructor and copy assignment operator (12.8), and destructor (12.4) are special member functions. The implementation will implicitly declare these member functions for a class type when the program does not explicitly declare them, except as noted in 12.1. The implementation will implicitly define them if they are used, as specified in 12.1, 12.4 and 12.8. Programs shall not define implicitly-declared special member functions. Programs may explicitly refer to implicitly declared special member functions. [Example: a program may explicitly call, take the address of or form a pointer to member to an implicitly declared special member function.

[...]

(bold emphasis mine)

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490108

Though I don't have a copy handy to check, I'd guess Lipmann means if you have something like:

myclass X;

...the X object will be initialized with the default ctor, regardless of whether the object is global (outside any function), local to a function, or local to some other scope within a function (e.g., in a for loop).

Contrast this with a built-in type line int, which has rather more complex rules: a global int (one defined outside any function) or one defined inside a function, but with static storage class, will be initialized to zero, but an int defined with automatic storage (defined inside a function, without specifying static) is "default initialized", which (in this case) means it's not initialized to a predictable value.

Upvotes: 3

paxdiablo
paxdiablo

Reputation: 881383

From the book itself:

Each class may also define what happens if a variable of the type is defined but an initializer is not provided. A class does so by defining a special constructor, known as the default constructor. This constructor is called the default constructor because it is run "by default;" if there is no initializer, then this constructor is used. The default constructor is used regardless of where a variable is defined.

(my italics).

So you're in a section of the book where they're already talking about default constructors (as should be evident in some of the other answers, the default constructor is most definitely not always used).

All that the book is saying is, in those situations where the default constructor is used, it makes no difference at all where the variable is defined (inside or outside functions, inside or outside classes, inside braces such as loops, selection statements or even naked braces, and so on).

Upvotes: 8

Joey Adams
Joey Adams

Reputation: 43380

If you have a class Object:

class Object
{
public:
    int x;

    Object() { x = 5; }
};

and you instantiate one in a function like this:

void foo()
{
    Object obj;
    // obj.x == 5
}

The default constructor will be used. Objects are also constructed with the default constructor when you declare them within another class and instantiate that other class:

class AnotherObject
{
public:
    Object obj;
};

void bar()
{
    AnotherObject another;
    // another.obj.x == 5
}

Upvotes: 7

Related Questions