R
Rolf Magnus
Alf said:* Victor Bazarov:Alf P. Steinbach said:[...]
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.
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.