Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Constructor call constructor

Reply
Thread Tools

Constructor call constructor

 
 
lallous
Guest
Posts: n/a
 
      03-05-2004
"Sławek" <(E-Mail Removed)> wrote in message
news:c29gd3$8ji$(E-Mail Removed)...
> Can one constructor of an object call another constructor of the same

class?
>
> class foo
> {
> foo(float f, int i) // a "full" constructor
> {
> ...
> }
> foo(int i) // a "simplified" constructor
> {
> ?? a call to foo(float,int), BTW this->foo(x,i) doesn't work ??
> }
> }
>
> 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 - with a
> similar architecture to the class foo. Is it possible to call a

constructor
> like a function?
>
> TIA
>
> Slawek
>


http://groups.google.com/groups?hl=e...fe=off&threadm
=burbmc%24lipge%241%40ID-161723.news.uni-berlin.de&rnum=1&prev=/groups%3Fq%3
Dlallous%2Bctor%26sourceid%3Dopera%26num%3D0%26ie% 3Dutf-8%26oe%3Dutf-8

Also:
"[10.3] Can one constructor of a class call another constructor of the
same class to initialize the this object?"
You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/
--
Elias


 
Reply With Quote
 
 
 
 
=?ISO-8859-2?Q?S=B3awek?=
Guest
Posts: n/a
 
      03-05-2004
Can one constructor of an object call another constructor of the same class?

class foo
{
foo(float f, int i) // a "full" constructor
{
...
}
foo(int i) // a "simplified" constructor
{
?? a call to foo(float,int), BTW this->foo(x,i) doesn't work ??
}
}

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 - with a
similar architecture to the class foo. Is it possible to call a constructor
like a function?

TIA

Slawek

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-05-2004
* <(E-Mail Removed)> 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?
 
Reply With Quote
 
Chris \( Val \)
Guest
Posts: n/a
 
      03-05-2004

"Sławek" <(E-Mail Removed)> wrote in message news:c29gd3$8ji$(E-Mail Removed)...
| Can one constructor of an object call another constructor of the same class?
|
| class foo
| {
| foo(float f, int i) // a "full" constructor
| {
| ...
| }
| foo(int i) // a "simplified" constructor
| {
| ?? a call to foo(float,int), BTW this->foo(x,i) doesn't work ??

Of course not .
You can however assign a temporary object to 'this':
*this = foo( 1.2f, i );

| }
| }
|
| 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 - with a
| similar architecture to the class foo. Is it possible to call a constructor
| like a function?

You cannot call a constructor directly like a function, full stop.
Constructors are 'invoked' upon object instantiation.

Why do you want to do this anyway ?

If you carefully thought out what you really want to
do, you would probably find that initialiser lists are
all you need.

Cheers.
Chris Val


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Constructor question, how does the call to the parent constructor work? marcwentink@hotmail.com C++ 6 05-09-2006 07:19 AM
How to call default constructor from other constructor of the same class? romayankin@gmail.com C++ 9 03-06-2006 09:30 PM
How to call a constructor from a constructor PengYu.UT@gmail.com C++ 10 05-09-2005 10:05 AM
call null constructor from another constructor Matt Graham C++ 3 02-07-2004 08:41 AM



Advertisments