Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > java like constructor calling constructor

Reply
Thread Tools

java like constructor calling constructor

 
 
lallous
Guest
Posts: n/a
 
      01-23-2004
Hello,

Probably I am not seeing it now, but is it possible for constructor(int) to
call constructor() ?

class A
{
private:
int x, y;
public:
A();
A(int);
};

A::A()
{
x = y = 0;
}

A::A(int x)
{
// call A::A() ?
// in Java, it is: this()
}

Or I have to write a small function like:
A::ctor()
{
// do common stuff here
}
A::A()
{
ctor();
}
A::A(int x)
{
ctor();
// more stuff here
}


--
Elias


 
Reply With Quote
 
 
 
 
Attila Feher
Guest
Posts: n/a
 
      01-23-2004
lallous wrote:
> Hello,
>
> Probably I am not seeing it now, but is it possible for
> constructor(int) to call constructor() ?

[SNIP]

No. It is being discussed as a possible future feature (delegating
constructor???) but right now all you can do is to make a 3rd (named)
function and call that one from both constructors. This - of course - lacks
the capability of sharing initializer lists.

--
Attila aka WW


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-23-2004
"lallous" <> wrote...
> Probably I am not seeing it now, but is it possible for constructor(int)

to
> call constructor() ?


No. Constructors don't have names, they cannot be called.

> [...]
>
> Or I have to write a small function like:
> A::ctor()


Yes.

V


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-23-2004
lallous wrote:

> Hello,
>
> Probably I am not seeing it now, but is it possible for
> constructor(int) to call constructor() ?


No.

> Or I have to write a small function like:


Either that, or use default values for the parameters of your A(int)
constructor.

 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      01-23-2004
On Fri, 23 Jan 2004 16:41:57 +0200, Attila Feher wrote:

> lallous wrote:
>> Hello,
>>
>> Probably I am not seeing it now, but is it possible for
>> constructor(int) to call constructor() ?

> [SNIP]
>
> No. It is being discussed as a possible future feature (delegating
> constructor???) but right now all you can do is to make a 3rd (named)
> function and call that one from both constructors. This - of course - lacks
> the capability of sharing initializer lists.


Well another thing that sometimes works is creating a baseclass that has
the common code in its constructor.

HTH,
M4

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      01-23-2004
On Fri, 23 Jan 2004 16:32:55 +0200 in comp.lang.c++, "lallous"
<> was alleged to have written:
>is it possible for constructor(int) to call constructor() ?


This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[10.3] Can one constructor of a class call another constructor of the
same class to initialize the this object?" It is always good to check
the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/


 
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
Calling parent constructor from child constructor pantalaimon C++ 3 10-09-2004 09:17 AM
Calling another constructor from a constructor Asfand Yar Qazi C++ 6 05-17-2004 03:16 PM
calling a constructor within a constructor Brett Irving C++ 3 06-29-2003 10:43 AM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57