By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

object creation questiions

P: n/a
Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

in main...:

MyClass();

// end

Q2:
Continuing from Q1 above, In the following code below, does the right
operand call the constructor and returns an object and then, the
constructor for x is called and then, the copy constructor is called to
copy the right object to x (regardless of optimizations)?: If not, what
is created first? the right operand or the left operand? In other
words; what does the code below do exactly?

MyClass x = MyClass();

// end

Jul 2 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
<xl***************@gmail.comwrote:
Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

in main...:

MyClass();

// end
That constructs a local object of type MyClass and discards it.
Rather useless. Like the following program:

int main(void)
{
7; // valid statement
return 0;
}

A complete C++ program. It will compile, link, and run on nearly
any platform. But... it does... absolutely... nothing.

Same with this:

#include<iostream>
void Func(){std::cout << "Hello, world!" << std::endl;}
int main()
{
Func; // valid statement
return 0;
}

Does... absolutely... nothing. Can you see why?
Q2:
Continuing from Q1 above, In the following code below, does the right
operand call the constructor and returns an object and then, the
constructor for x is called and then, the copy constructor is called to
copy the right object to x (regardless of optimizations)?: If not, what
is created first? the right operand or the left operand? In other
words; what does the code below do exactly?

MyClass x = MyClass();
That's the canonical form for getting "default initialization" for
local objects. I'm not sure if a MyClass object would have all
of its members set to zero, or to values specified by the constructor,
if you just said:

MyClass x;

Perhaps others here could tell you more about this.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 2 '06 #2

P: n/a
xl***************@gmail.com wrote:
Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

in main...:

MyClass();

// end
Expression 'MyClass()' creates a value-initialized temporary object of class
type 'MyClass'. There's no way to say whether the default constructor of
'MyClass' is called or not in this case without knowing more details about the
class. The answer depends on whether the class has a _user-declared_ constructor.
Q2:
Continuing from Q1 above, In the following code below, does the right
operand call the constructor and returns an object
The initializer expression returns a value-initialized temporary object, as
described above (and, once again, there's no way to say whether the constructor
is called or not).
and then, the
constructor for x is called and then, the copy constructor is called to
copy the right object to x (regardless of optimizations)?
No. Only the copy-constructor for x is called to copy the temporary 'MyClass()'
to 'x'. Default constructor for 'x' is not called. This sequence of steps can be
referred to as "long" initialization.

However, the compiler is allowed to optimize the code and eliminate the
temporary. I.e. the optimized version of this code would simply call default
constructor for 'x' and do nothing else. That would be the "short" initialization.

: If not, what
is created first? the right operand or the left operand?
If it decides to go the "long" way (with the temporary and copying), then the
temporary is created first, of course. The temporary is required to start
creating 'x' (as a parameter for 'x's copy constructor), so it has to be created
before.
In other
words; what does the code below do exactly?

MyClass x = MyClass();
See above. What it will do exactly depends on which optimizations the compiler
will decide to perform" go the "long" way or the "short" way.

--
Best regards,
Andrey Tarasevich
Jul 2 '06 #3

P: n/a
Andrey Tarasevich wrote:
xl***************@gmail.com wrote:
>Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

in main...:

MyClass();

// end

Expression 'MyClass()' creates a value-initialized temporary object of
class type 'MyClass'. There's no way to say whether the default
constructor of 'MyClass' is called or not in this case without knowing
more details about the class. The answer depends on whether the class has
a _user-declared_ constructor.
No, it doesn't. This always calls the default constructor of MyClass. If
there is no user-defined default constructor, the compiler generates one
for you. If you only declare and not define the default constructor, you'll
get a compile error.
>Q2:
Continuing from Q1 above, In the following code below, does the right
operand call the constructor and returns an object

The initializer expression returns a value-initialized temporary object,
as described above (and, once again, there's no way to say whether the
constructor is called or not).
Whenever an object of a class type is created, a constructor of that class
is called.

Jul 2 '06 #4

P: n/a
Rolf Magnus wrote:
Andrey Tarasevich wrote:
>xl***************@gmail.com wrote:
>>Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

in main...:

MyClass();

// end
Expression 'MyClass()' creates a value-initialized temporary object of
class type 'MyClass'. There's no way to say whether the default
constructor of 'MyClass' is called or not in this case without knowing
more details about the class. The answer depends on whether the class has
a _user-declared_ constructor.

No, it doesn't. This always calls the default constructor of MyClass. If
there is no user-defined default constructor, the compiler generates one
for you. If you only declare and not define the default constructor, you'll
get a compile error.
I'm sure you know this, but to be very clear, the compiler generates a
default constructor only if there is no user-defined constructor of any
type (default or not).

Mark
Jul 2 '06 #5

P: n/a
posted:
Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

class MyClass {
private:

int i;

public:

MyClass() : i(-1) {}

};

int main()
{
MyClass thing1;

/* Creates a local object by invoking
its default constructor. (NB: If this
were a POD, then it would contain
white noise. */
MyClass thing2();

/* Declares a function called "thing2"
which returns a MyClass by value,
and which doesn't take any parameters. */
MyClass thing3(void);

/* Identical to the previous declaration. */
MyClass();

/* Creates a non-const, R-value, name-less
object of type "MyClass" which will be
destroyed at the end of the statement. */
}
--

Frederick Gotham
Jul 2 '06 #6

P: n/a
Robbie Hatley wrote:
Rather useless. Like the following program:

int main(void)
{
7; // valid statement
return 0;
}

A complete C++ program. It will compile, link, and run on nearly
any platform. But... it does... absolutely... nothing.
It returns without error 100% of the time -- that ain't nothin'.

Luke

Jul 2 '06 #7

P: n/a
Rolf Magnus wrote:
Andrey Tarasevich wrote:
>xl***************@gmail.com wrote:
>>Q1:
What happens when you apply parenthesis to the class type name? Is the
constructor for the class called and returns an object?, or what?

in main...:

MyClass();

// end

Expression 'MyClass()' creates a value-initialized temporary object of
class type 'MyClass'. There's no way to say whether the default
constructor of 'MyClass' is called or not in this case without knowing
more details about the class. The answer depends on whether the class has
a _user-declared_ constructor.

No, it doesn't. This always calls the default constructor of MyClass.
No. That's incorrect. In post-TC1 language the '()' initializer will cause a
call to the constructor if and only if that constructor is user-declared (and
defined, of course). Otherwise, the value-initialization is performed _without_
the use of the top-level constructor.

For example, consider the following class

struct S { int x, y; };

This class has no user-declared constructor, which means that the compiler will
provide one implicitly. However, the compiler-provided constructor will
essentially do nothing (as specified in the standard). It will not initialize
S::x' and 'S::y' to any particular values.

Now consider the expression 'S()'. This expression is guaranteed to produce an
'S' object with zero-initialized 'S::x' and 'S::y'. What do you think does the
initialization? The default constructor of 'S'? No. The default constructor of
'S' does nothing of that nature. What happens in this case is an independent
initialization process that doesn't involve the constructor of 'S'.

That, BTW, how it was in pre-TC1 standard as well. Except that in pre-TC1 one
the line between "constructor called" and "_constructorless_ default
initialization performed" was drawn between non-POD and POD types. TC1 changed
that by introducing value-initialization and moving the line to the "has
user-declared constructor" and "has no user-declared constructor" position.

Read the description of '()' initialization in either version of the standard.
It is easy to see that the default constructor is not called for POD class
types, for example.

Moreover, for POD class types the existence of implicit default constructor is
postulated for consistency only. Even at the conceptual level, that constructor
is never called at all. Expression 'S()' does not call it. Declaration

S s;

still does not call the constructor (see 8.5/9), even though the result
(indeterminate values) is consistent with the constructor's behavior.
If
there is no user-defined default constructor, the compiler generates one
for you.
Yes. But that does not mean that it will be used in '()' initialization.

If you only declare and not define the default constructor, you'll
get a compile error.
That's understood, but that's not what I'm talking about.
...
Whenever an object of a class type is created, a constructor of that class
is called.
...
The above is true only for classes with user-declared constructors. But in
general case - no. Not in C++ language.

--
Best regards,
Andrey Tarasevich
Jul 2 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.