Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to call default constructor from other constructor of the same class?

Reply
Thread Tools

How to call default constructor from other constructor of the same class?

 
 
romayankin@gmail.com
Guest
Posts: n/a
 
      03-06-2006
Here is the problem. There are two constructors of the same class
~~~~~~~~~~~~
CClass()
: param1 (0.5),
param2 (100000),
param3 (NULL),
szPath (NULL)
{

}
CClass(char *path)
: szPath(path)
{

}
//Assume we have the following object
CClass cc_path(szSomePath);
~~~~~~~~~~~~

Is it possible to call CClass() initialization list, before call to
CClass(char *path)? Well an obvious workaround is to create some
private member function that will contain all instantiations but is it
possible to do something leaving everything intact and just adding some
code?

 
Reply With Quote
 
 
 
 
Greg
Guest
Posts: n/a
 
      03-06-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Here is the problem. There are two constructors of the same class
> ~~~~~~~~~~~~
> CClass()
> : param1 (0.5),
> param2 (100000),
> param3 (NULL),
> szPath (NULL)
> {
>
> }
> CClass(char *path)
> : szPath(path)
> {
>
> }
> //Assume we have the following object
> CClass cc_path(szSomePath);
> ~~~~~~~~~~~~
>
> Is it possible to call CClass() initialization list, before call to
> CClass(char *path)? Well an obvious workaround is to create some
> private member function that will contain all instantiations but is it
> possible to do something leaving everything intact and just adding some
> code?


No, at least not in the current C++ standard. Perhaps the next major
revision of C++ will support "delegating" constructors (much like Java
does).

But about the best that you can do currently, is to consolidate the
initialization logic in a common method, usually called Init() or
something similar. Not an ideal solution, but not a horrible one
either, in my opinion.

Greg

 
Reply With Quote
 
 
 
 
mbavishal@gmail.com
Guest
Posts: n/a
 
      03-06-2006
Semantics of intialization list does not allow calling a constructor
from other constructor of the same class.

And the workaround you are talking about is not pure intializtion it is
an assignment.

regards
vishal sharma

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-06-2006
* (E-Mail Removed):
> Is it possible to call CClass() initialization list, before call to
> CClass(char *path)?


No.

This is a FAQ,
<url: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.3>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Neelesh Bodas
Guest
Posts: n/a
 
      03-06-2006

(E-Mail Removed) wrote:
> Here is the problem. There are two constructors of the same class
> ~~~~~~~~~~~~
> CClass() : param1 (0.5), param2 (100000), param3 (NULL), szPath (NULL)
> {}


> CClass(char *path) : szPath(path)
> {}


> //Assume we have the following object
> CClass cc_path(szSomePath);
> ~~~~~~~~~~~~
>
> Is it possible to call CClass() initialization list, before call to
> CClass(char *path)?


I was wondering why would you want to do that at the first place (May
be you have no control over the source code, but then what was the
original reason to skip initialization of other member variables in the
second constructor?

>Well an obvious workaround is to create some
> private member function that will contain all instantiations


no, thats not a workaround. You are talking about assignment and
actually expecting an initialization.

> but is it possible to do something leaving everything intact and just adding some
> code?


CClass(char *path) : szPath(path) ,param1 (0.5), param2 (100000),
param3 (NULL)
{}

Oh, I believe thats not what you expected, by I "just added some code,
leaving everything intact" you see ...

 
Reply With Quote
 
roberts.noah@gmail.com
Guest
Posts: n/a
 
      03-06-2006

(E-Mail Removed) wrote:
> but is it
> possible to do something leaving everything intact and just adding some
> code?


Sure there is. In CClass(char * path) after the : and before
szPath(path) add the following code:

param1 (0.5),
param2 (100000),
param3 (NULL),

 
Reply With Quote
 
romayankin@gmail.com
Guest
Posts: n/a
 
      03-06-2006
>> what was the original reason to skip initialization of other member variables in the
second constructor?
Neelesh, I didn't want to have the same code in two places.


I was able to come up with following ugly option:
~~~~~~~~~~~~~~~
CClass()
: param1 (0.5),
param2 (100000),
param3 (NULL),
szPath (NULL)
{

}
CClass(char *path)
{
*this = CClass();
//copy path to szPath here
}
~~~~~~~~~~~~
but just because, there's nothing better then a simple and straight
option i'll copy the initialization list to the second constructor as
well.
I guess that's it. Thank you All!

-R.

 
Reply With Quote
 
Fei Liu
Guest
Posts: n/a
 
      03-06-2006

(E-Mail Removed) wrote:
> >> what was the original reason to skip initialization of other member variables in the

> second constructor?
> Neelesh, I didn't want to have the same code in two places.
>
>
> I was able to come up with following ugly option:
> ~~~~~~~~~~~~~~~
> CClass()
> : param1 (0.5),
> param2 (100000),
> param3 (NULL),
> szPath (NULL)
> {
>
> }
> CClass(char *path)
> {
> *this = CClass();
> //copy path to szPath here
> }
> ~~~~~~~~~~~~
> but just because, there's nothing better then a simple and straight
> option i'll copy the initialization list to the second constructor as
> well.
> I guess that's it. Thank you All!
>
> -R.


What about default parameter list?
CClass(float param1 = 0.5, int param2 = 100000, char * param3 = 0, char
* szPath = 0){
.....
}

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      03-06-2006
Fei Liu <(E-Mail Removed)> wrote:
> What about default parameter list?
> CClass(float param1 = 0.5, int param2 = 100000, char * param3 = 0, char
> * szPath = 0){
> ....
> }


I suggested this to someone else in a different situation, and they
pointed out that default parameters can affect the interface. For this
example, this default parameter list will have the effect of making
available the following constructors, which may not necessarily be
desirable:

CClass();
CClass(float param1);
CClass(float param1, int param2);
CClass(float param1, int param2, char* param3);
CClass(float param1, int param2, char* param3, char* szPath);

For example, the class may be designed such that if any of {param1,
param2, param3} are provided, then all of them must be provided, in
which case the extra constructors should not be there.

--
Marcus Kwok
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-06-2006
* Marcus Kwok:
> Fei Liu <(E-Mail Removed)> wrote:
>> What about default parameter list?
>> CClass(float param1 = 0.5, int param2 = 100000, char * param3 = 0, char
>> * szPath = 0){
>> ....
>> }

>
> I suggested this to someone else in a different situation, and they
> pointed out that default parameters can affect the interface. For this
> example, this default parameter list will have the effect of making
> available the following constructors, which may not necessarily be
> desirable:
>
> CClass();
> CClass(float param1);
> CClass(float param1, int param2);
> CClass(float param1, int param2, char* param3);
> CClass(float param1, int param2, char* param3, char* szPath);
>
> For example, the class may be designed such that if any of {param1,
> param2, param3} are provided, then all of them must be provided, in
> which case the extra constructors should not be there.


Well, you can always use a private base class, or a private class for a
single data member providing the relevant member items.

E.g., for the OP's example (I abhor this naming convention, but use the
exact same example),

class CClass
{
private:
struct DataMembers
{
double param1;
long param2;
void const* param3;
char const* szPath;

DataMembers( char const* path = NULL )
: param1( 0.5 )
, param2( 100000 )
, param3( NULL )
, szPath( path )
{}
} m;

public:
CClass() {}
CClass(char *path): m( path ) {}
};

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Mixing default and non-default parameters in Constructor? desktop C++ 2 06-06-2007 08:25 PM
Bug? Parametrized constructor invokes default constructor mario.rossi@REMOVETHISTOMAILMENOT.com C++ 4 10-12-2006 07:59 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments