im*****@hotmail.co.uk wrote:
>Constructors do NOT create object in C++.
Eh, well what do they do then ?
They perform initialization.
>
>You can't call them.
Are you sure you are right ?
Absolutely. The words come straight out of the standard. Constructors
do not participate in name resolution. They can't be called by code.
Any syntax that appears to do so is either illegal or doing something
else.
For instance:
class X {
public:
X() { }
X(int a) {
X();
}
};
The line in the second (non-default) constructor isn't calling anything.
It makes a temporary default constructed temporary object that vanishes
when the expression it is used ends.
Just to be pedantic I thought I saw on the
faq that some people did this by calling one constructor from another,
but the faq did say it was a very bad thing to do this, but it did not
say that it was not possible.
The FAQ does not say it. It's not just a bad thing, it's not possible.
If you have code that is common to more than one constructor you need
to put it in another member function that can be called.
There are some people who espouse hacks such as placement constructing
an object on top of the one under construction but it is frowned upon
and also involves implementation-specific or undefined behavior.
>
Confusing, well what would you call it then (the normal constructor?)
look at this object pascal as a comparison:
I didn't invent the terms. But default constructor is one called with
no arguments whether its generated by the compiler OR one you write.
>
The Create call calls the underlying constructor.
OK, is it called the normal constructor ? But if it is empty how is
the object created ?
It is important for you to realize that the CREATION: be it as
a result of a locally declared variable, a temporary, or dynamic
creation via new embodies two things:
1. Allocation of storage for the object (which you really don't
have any control over other than the context of the way you
create the object, this code is all automagically emitted by the
compiler for you).
2. Initialization, which involves invoking constructors for
class objects and doing other initialization for more primitive
types.
he standard uses the terms "implicitly declared" and "implicitly
defined" for the four functions it will generate for you in certain
circumstances:
a default constructor (if no constructors are declared)
a copy constructor (if no copy constructor is declared)
a copy-assignment operator (if not declared)
a destructor (if not declared)
>
And if you leave out those objects then they won't get created
correctly, or something else ?
No, if they are class objects, their default constructors (implicit
or explicitly defined) are invoked. If they are not class objects
then rather stupid and arcane initialization rules are invoked
depending on the context (it is a major freaking defect in C++
that default construction of non-class objects isn't always the
same, but too much whining and crying by ex-C programmers ruined
this).