Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How to call default constructor from other constructor of the same class? (http://www.velocityreviews.com/forums/t452319-how-to-call-default-constructor-from-other-constructor-of-the-same-class.html)

romayankin@gmail.com 03-06-2006 04:35 AM

How to call default constructor from other constructor of the same class?
 
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?


Greg 03-06-2006 05:03 AM

Re: How to call default constructor from other constructor of the same class?
 
romayankin@gmail.com 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


mbavishal@gmail.com 03-06-2006 05:07 AM

Re: How to call default constructor from other constructor of the same class?
 
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


Alf P. Steinbach 03-06-2006 05:21 AM

Re: How to call default constructor from other constructor of thesame class?
 
* romayankin@gmail.com:
> 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?

Neelesh Bodas 03-06-2006 05:30 AM

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

romayankin@gmail.com 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 ...


roberts.noah@gmail.com 03-06-2006 06:54 AM

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

romayankin@gmail.com 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),


romayankin@gmail.com 03-06-2006 08:33 AM

Re: How to call default constructor from other constructor of the same class?
 
>> 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.


Fei Liu 03-06-2006 08:11 PM

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

romayankin@gmail.com 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){
.....
}


Marcus Kwok 03-06-2006 09:14 PM

Re: How to call default constructor from other constructor of the same class?
 
Fei Liu <fei.liu@gmail.com> 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

Alf P. Steinbach 03-06-2006 09:30 PM

Re: How to call default constructor from other constructor of thesame class?
 
* Marcus Kwok:
> Fei Liu <fei.liu@gmail.com> 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?


All times are GMT. The time now is 12:44 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.