Calling a constructor from another

S

Sathyaish

How does a constructor of one class call another of the same class?
When would this mechanism make sense or be required?


PS: Two instances I saw are:

(a) While using the Singleton pattern
(b) While using the Factory pattern
 
S

Stephen Howe

How does a constructor of one class call another of the same class?

It can't. C++ is not Java.

Stephen Howe
 
J

Jerry Coffin

Sathyaish said:
How does a constructor of one class call another of the same class?

About like anything else does -- and the result is the same as well --
doing so constructs a new object.
When would this mechanism make sense or be required?

Virtually never. The only time it makes sense is if constructing one
object somehow involves first constructing another object of the same
type. While I can see theoretical possibilities for this (e.g. creating
a default-contructed object an then copying it to the current object to
initialize most members) I've never actually seen a situation where
this seemed like a good idea in real life.
PS: Two instances I saw are:

(a) While using the Singleton pattern
(b) While using the Factory pattern

I suspect what you're looking for is slightly different. If you have
two ctors that have a consderable amount of common code, you do NOT
normally want one ctor to call the other. Rather, you want to create a
(usually private) function containing the common code, and then have
both ctors call that common initialization function.

As a final aside: officially you can't ever "call a constructor" -- you
can only create an object, and the constructor gets invoked
automatically in the process of constructing the object.
 
A

Alf P. Steinbach

* Jerry Coffin:
As a final aside: officially you can't ever "call a constructor" -- you
can only create an object, and the constructor gets invoked
automatically in the process of constructing the object.

I've given up on some others, who after acknowledging the error of their
ways in debate then immediately forget it, so that the same arguments and
facts have had to be repeated later on, and then later on, and... It's
religious or some sort of imagined group membership, I think. But for the
record: the above is not "officially". It's "unofficially", as in: "in the
misleading terminology and internally inconsistent belief-set used by a
small group of folks frequenting this newsgroup, who have a habit of
immediately forgetting it when confronted with reason and the standard".

It is of course valid to oversimplify in teaching, but then one should IMO
be _very_ clear about this being an oversimplification.

And it is IMO simply wrong to use _misleading_ explanations, whether one
explains that they're oversimplified or not.

A less simplified explanation of the above: the language rules are designed
so that, unless you use very low-level constructs designed to provide a
loop-hole, you are guaranteed that every object of class type T receives
exactly one T constructor call, before anything else happens to that object
(which implies that you cannot ordinarily call a T constructor without
creating a T object, or vice versa).

Now even that is an oversimplification, but not a misleading one: it's good
enough as working model.
 
J

Jerry Coffin

Alf said:
* Jerry Coffin:

I've given up on some others, who after acknowledging the error of
their ways in debate then immediately forget it, so that the same
arguments and facts have had to be repeated later on, and then later
on, and... It's religious or some sort of imagined group membership,
I think. But for the record: the above is not "officially". It's
"unofficially", as in: "in the misleading terminology and internally
inconsistent belief-set used by a small group of folks frequenting
this newsgroup, who have a habit of immediately forgetting it when
confronted with reason and the standard".

You're right -- I started out by saying something on the order of
"officially a ctor doesn't have a name, and you don't call it directly"
but in trying to shorten that, I also made it less than accurate.

While it's entirely possible that I'll make the same mistake again some
day, I hope you'll take my assurance that if I do, it has more to do
with being absent minded than anything having to do with religion or
group membership -- and I hope you (or at least somebody) will correct
me when/if I do.

In any case, I hope I didn't mislead anybody too badly, and certainly
apologize if I did.
 
R

Rolf Magnus

Alf said:
* Jerry Coffin:

I've given up on some others, who after acknowledging the error of their
ways in debate then immediately forget it, so that the same arguments and
facts have had to be repeated later on, and then later on, and...

If I am one of those "others", then you should know that I never
acknowledged what you call an "error". I simply disagree with you, and I
still think you're the one who is wrong. However, I also gave up on trying
to convince you of that.
It's religious or some sort of imagined group membership, I think.

Bogus. It's just that more people have another view on this than you.
But for the record: the above is not "officially". It's "unofficially",
as in: "in the misleading terminology and internally inconsistent
belief-set used by a small group of folks frequenting this newsgroup,
who have a habit of immediately forgetting it when confronted with
reason and the standard".

Stop being so arrogant. We had those discussions, and you also were
confronted with reason and the standard, and you also seemed to have
forgotten those things. IMO, you are just interpreting the references to
the standard that you gave in an incorrect way. I told you that, you
ignored it. So don't point your finger at others.
It is of course valid to oversimplify in teaching, but then one should IMO
be _very_ clear about this being an oversimplification.

And it is IMO simply wrong to use _misleading_ explanations, whether one
explains that they're oversimplified or not.

Yes, I agree on that. However, this is not one case of a misleading
explanation.
A less simplified explanation of the above: the language rules are
designed so that, unless you use very low-level constructs designed to
provide a loop-hole, you are guaranteed that every object of class type T
receives exactly one T constructor call, before anything else happens to
that object (which implies that you cannot ordinarily call a T constructor
without creating a T object, or vice versa).

Still, even if you use those loop-hole constructs, you must ensure that each
object only receives one constructor call if you want your program to
behave in a defined way.
 
S

santosh

Hello,
I have two doubts on C++.
1. float i=1.1;
double d=1.1;
if(i==d)
printf("equal\n");
else
printf("not-equal\n");
what is the output?
I am getting "not-equal" as output.
Why it is so?
 
V

Vyacheslav Kononenko

santosh said:
Hello,
I have two doubts on C++.
1. float i=1.1;
double d=1.1;
if(i==d)
printf("equal\n");
else
printf("not-equal\n");
what is the output?
I am getting "not-equal" as output.
Why it is so?

Short answer whould be "you should not use operator== to compare
floating point values, unless you know what you are doing" The problem
is floating point value is an approximation and you cannot use exact
comparison on approximate values (actually you can but you will not get
expected results). You should use something like if( fabs( v1 - v2 ) <=
delta ) ... where delta depends on your application. If you need exact
values you should use fixed point calculations and values. They are not
directly supported by the language but you can easily find appropriate
library for you.

Regards,
Vyacheslav
 
A

Alf P. Steinbach

* Rolf Magnus:
Still, even if you use those loop-hole constructs, you must ensure that each
object only receives one constructor call if you want your program to
behave in a defined way.

It's that imprecise use of language & logic that allows you to hold an
inconsistent belief, or at least, to argue for it.

Contrary to your statement, an object may receive _any_ number of
constructor calls in a well-defined program. Except if 'receive' is defined
so that all calls except one are excluded from being 'received', which is
then circular reasoning. I.e., your statement is extremely misleading,
asserting something that is false with a natural interpretation of the
language, and meaningless with an interpretation so that it's not false.

An object of class type T will, except for use of the mentioned constructs,
receive exactly one T constructor call.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,203
Messages
2,571,059
Members
47,668
Latest member
SamiraShac

Latest Threads

Top