Re: constructor

From: Rolf Magnus (ramagnus_at_t-online.de)
Date: 11/24/04


Date: Wed, 24 Nov 2004 15:44:02 +0100

Alf P. Steinbach wrote:

> * Victor Bazarov:
>> "Alf P. Steinbach" <alfps@start.no> wrote...
>> > [...]
>> > Every tree of constructor calls start with a source code level
>> > constructor call, it's just a very common misconception that they're
>> > never "explicit", or not "calls" (that last misconception is mentioned
>> > in the FAQ, by ref to the definition of default constructor). The
>> > first misconception probably stems from a passage in the standard where
>> > the word "explicit" is used for disambiguation in a particular context,
>> > and the last misconception probably stems from the desire to teach
>> > newbies not to think of a constructor as a an ordinary callable member
>> > function, but doing that by inventing a simple and incorrect
>> > explanation instead of how things actually work.
>>
>> 12.1/2 "Because the constructors do not have names, they are never found
>> during name lookup". Yes, you can _cause_ a constructor to be called.
>
> That's correct.

Yes.

>> You can never _call_ one, though.
>
> That's incorrect.

No. Let's see how a C++ programmer calls a function. I see two ways:

1. Write function's name, followed by '(', a parameter list and ')'.
2. Through a pointer, which is similar, except that instead of the
function's name, you use a pointer to it. However, to make a pointer point
to it, you need to have the function's name too.

We could get a bit deeper by adding member function syntax and stuff like
that, but that isn't really significant here. The important part is that
both ways require you to have the function's name. Now let's look again at
the standard quote from above. It clearly says "constructors do not have
names", so you cannot call a constructor. It's that simple.
That, however doesn't mean that constructors are never called. If they
weren't, why would they exist? If you have a function:

void foo()
{
    bar();
    baz();
}

and you call it like:

int main()
{
    foo();
}

then you can say that you called foo() from main(). You cannot say you
called bar() or baz() from main, but they still get called - as a result of
foo() being called. The same is true for constructors. If you write:

#include <iostream>

struct X
{
    X() { std::cout << "Hello, world\n"; }
};

int main()
{
    X x;
}

again, you didn't call a constructor from main(). You created an object. And
as a result of that object creation, the constructor is called.
There is - however - a difference to the example before: While you could
call bar() and baz() yourself (i.e. directly from main()), you cannot call
a constructor yourself (i.e. directly from main()). It will be called for
you automatically as part of the object creation.

> I seem to recall we have discussed that before, and so I simply direct
> you again to the same place (definition of default constructor), "can be
> called".

Yes, of course it can be called, but not by the programmer.

>> Where's the misconception?
>
> In your mind... ;-)

Hmm, haven't you noticed that there doesn't seem to be any other regular
here that shares your view on this topic, and that you get involoved in
discussions of that kind regularly because each time several others think
that you're explaining incorrect things to novice programmers? Have you
ever considered that the misconception might be in your mind, not in the
minds of everyone else?
Maybe we should take that discussion to comp.std.c++ and hear what the
standards people have to say about it, to end this once and for all.



Relevant Pages

  • Re: C++ design question
    ... so anyone can instantiate it without ... > default constructor in the class header rather that allowing it to be ... > separately and initialize a pointer to it in Foo: ... fooDeriveN::DoStuffWithBarBase() at all because we have a Bar* in Foo*. ...
    (comp.object)
  • Re: C++ design question
    ... The default constructor is implied in the initialization list. ... One wants to access Bar at the superclass level to avoid static typing ... Usually that would be done by whoever instantiates a Foo subclass ...
    (comp.object)
  • Re: "x.constructor == Foo" vs "x instanceof Foo"
    ... (x instanceof Foo) ... by examining the value of its first argument's the constructor ... as giving the object graph: ...
    (comp.lang.javascript)
  • Re: getting used to Java - question about "style"
    ... > constructor." ... Let's suppose you create a class Foo ... ... They can also override frob(), because we didn't make it private or final. ... When a new object of type Bar is created, ...
    (comp.lang.java.programmer)
  • Re: New (as in days) to Java - question about "super()" method
    ... > it does make good basic sense to get everything, ... If you have a class Foo that extends Bar, ... You need to have the Bar object in place before you can start ... If your constructor uses no arguments, ...
    (comp.lang.java.programmer)