473,322 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Constructor call constructor

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

Jul 22 '05 #1
3 2227
"S³awek" <sl****@dev.null> wrote in message
news:c2**********@zeus.man.szczecin.pl...
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
Jul 22 '05 #2
* <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?
Jul 22 '05 #3

"S³awek" <sl****@dev.null> wrote in message news:c2**********@zeus.man.szczecin.pl...
| 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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
3
by: mhvaughn | last post by:
struct S1 { int i; }; struct S2 { S1 s; // version 1 S2() {} ; // version 2
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.