473,326 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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?

Mar 6 '06 #1
9 2024
ro********@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

Mar 6 '06 #2
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

Mar 6 '06 #3
* ro********@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?
Mar 6 '06 #4

ro********@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 ...

Mar 6 '06 #5

ro********@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),

Mar 6 '06 #6
>> 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.

Mar 6 '06 #7

ro********@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){
.....
}

Mar 6 '06 #8
Fei Liu <fe*****@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
Mar 6 '06 #9
* Marcus Kwok:
Fei Liu <fe*****@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?
Mar 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and...
10
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but...
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
6
by: Gunnar G | last post by:
If I don't define a default constructor for my class "Foo", I still get one from the compiler. But if I define a constructor that takes an argument, I don't get the default constructor, why? I...
8
by: trying_to_learn | last post by:
Why do we need to explicitly call the copy constructor and the operator = , for base class and member objects in composition? ....book says "You must explicitly call the GameBoard copy-constructor...
7
by: AndreasW | last post by:
Hi, internal static class Program { public class foo { private int _id; public foo() {
3
by: Mark | last post by:
So, I was looking at this code example by Bruce Eckel where he hides the copy constructor by making it private... but in his code I noticed that he writes NoCC n(); *with* parentheses. Now...
7
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.