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