constructor call

Pages: 12
Hello.

Why is it that constructor is called just once in the following code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct widget{
widget(){cout << "ctor called" << endl;}
};


widget f(widget u)
{
       widget V(u);
       widget W = V;
       return W;
}
int main()
{
    cout << "Here" << endl;
    widget x;
    cout << "Here" << endl;
    widget y = f(f(x));

    
    
    
    for(int i  = 0;i<420000000;++i);

    return 0;
}
that constructor
?
'That' is only a conjunction here
x is constructed once. All the other "constructor" calls are copy ctor calls.
How many times is the ctor called?
And, would you please write a code that demonstrates it exactly?
Last edited on
Why can I not read the word 'conjunction' without getting the conjunction-junction song stuck in my head? :(

How many times is the ctor called?

Which constructor? The default constructor (which you have explicitly defined), the copy constructor, or both? Add the below to your struct to see copy constructor calls. Note: because of compiler optimizations, you may see far less than you expect.

widget(const widget & _cpy){cout << "copy called" << endl;}
Your snippet errors
Looks okay to me - can you post all your code with this change?
struct widget{
widget(){cout << "ctor called" << endl;}
widget(const widget & _cpy){cout << "copy called" << endl;}
};


widget f(widget u)
{
widget V(u);
widget W = V;
return W;
}
int main()
{
widget x;
cout << "Here" << endl;
widget y = f(f(x));




for(int i = 0;i<420000000;++i);

return 0;
}

Also, I assume ctor means default ctor.A class is not assumed to have a copy ctor, isn't it?
The above compiles for me...what error are you getting? (warnings about _cpy being unreferenced aren't important...you can just omit the name '_cpy' if you care).

Simple classes have 4 things automatically defined by the compiler: a default constructor, a copy constructor, an assignment operator, and a destructor. So yes, it is assumed to have a copy constructor. Note there are various cases where some of these are NOT generated by the compiler, but that's another topic. Also, C++11 adds a couple other compiler-generated methods, but again another topic ;)
Have you read it anywhere in particular that there are two types of ctor?If so, please tell me to report an error in the question.
The original question didn't have the struct definition
Last edited on
If your professor wants to know the number of constructors, and you are given both main and f():
There is one default constructor, when x is defined. There may be up to 3 copy constructors for the first call to f(), along with one more potentially when f() returns, whose return value is then used in the 2nd call to f(). In this second call, there may be up to 3 copy constructors called as well. However, if a move constructor is defined for the struct in question, because the passed in parameter is an rvalue, there will be at most 1 move constructor and 2 copy constructors called, along with potentially 1 more moving the return value from the 2nd f() to a temporary, and 1 last move constructor (or copy, if move not defined) when constructing y. However, due to RVO, many of these copies may be elided, including all copies to temporaries outside of functions, and even the copies to the parameter of f(), resulting in only 2 copies being required for each call to f() (for a total of 4), plus one for the eventual construction of y.
But where did you read that there are two types of constructor?I have to know that, to report it as an error question(in the entrance exam).
You can read about copy constructors everywhere. Even Wikipedia has an article about it http://en.wikipedia.org/wiki/Copy_constructor

If you are using C++11 a move constructor could also be involved.
But if I should report the question as error, I have to name an authoriatitive source
Copy constructors are described in §12.8 in the C++ standard.
closed account (zb0S216C)
hooshdar3 wrote:
"But where did you read that there are two types of constructor?"

Constructors are described in every C++ book, even the bad ones. There's more than 2 types:

- Default Constructor:

1
2
3
4
5
struct Object
{
    Object(); // Default Constructor A
    Object(int param_ = 0); // Default Constructor B
};

- Copy-Constructor:

1
2
3
4
5
struct Object
{
    Object(Object &object_); // Copy Constructor A
    Object(Object const &object_); // Copy Constructor B - Best common practice
};

- Normal Constructor:

1
2
3
4
struct Object
{
    Object(int a_); // Normal Constructor
};

- Move-Constructor (C++11):

1
2
3
4
struct Object
{
    Object(Object &&object_); // Move-Constructor
};

Wazzak
By "Normal Constructor" Framework (probably) means that you can have constructors for any types you want as arguments with the same freedom as with functions.
Last edited on
Copy constructors are described in §12.8 in the C++ standard.

Where can I get C++ standard?
Last edited on
+I got the idea from reading Herbet Shieldt's text that copy constructor is optional.true?
Pages: 12