* <sl****@dev.null> schriebt:
Can one constructor of an object call another constructor of the same class?
This is a FAQ.
It's always a good idea to check the FAQ before posting:
<url: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.3>
Obviously it's possible to use a base class with the "full" constructor or
write private function (foo_init(float,int) called via foo(float,int) and
foo(int i) ). Nevertheless I look for an alternative solution.
None such except providing default values for arguments, directly in the
constructor code or via factory functions.
Is it possible to call a constructor like a function?
So many (most C++ programmers... :-; ) have problems with the questions you
ask above that it's near to impossible to answer this correctly without
incurring the indignant wrath of those who have just progressed past the
immediate basic understanding that constructors for the same class can't be
chained in C++.
The usual over-simplification is: "you can't call a constructor".
This simplification serves well for novices and the 50% of programmers below
the median skill level, and it is perhaps the rule-of-thumb you should adopt.
A slightly less incorrect answer is: "you can't call a constructor on an
object", and this conveys the main idea. There is no object before the
constructor has done its job. The constructor transforms raw storage into
a useful object.
But also that is slightly incorrect, for you can call a constructor on
raw storage via placement new. C++ has its roots in low-level programming
and so provides this way to take charge. And in the standard's terminology
raw storage is also regarded as 'object'. Nailing down just the precise
meaning of object you can't call a constructor on is hard. But in practice
the possibility of placement new is just that: it's simply not used, because
there are so few situations where it could be safe or an advantage to use it.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?