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

R

romayankin

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?
 
G

Greg

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
 
M

mbavishal

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
 
N

Neelesh Bodas

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 ...
 
R

roberts.noah

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),
 
R

romayankin

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.
 
F

Fei Liu

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){
.....
}
 
M

Marcus Kwok

Fei Liu said:
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.
 
A

Alf P. Steinbach

* Marcus Kwok:
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 ) {}
};
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top