473,809 Members | 2,777 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(szSomeP ath);
~~~~~~~~~~~~

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 2044
ro********@gmai l.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(szSomeP ath);
~~~~~~~~~~~~

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********@gmai l.com:
Is it possible to call CClass() initialization list, before call to
CClass(char *path)?


No.

This is a FAQ,
<url: http://www.parashift.c om/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********@gmai l.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(szSomeP ath);
~~~~~~~~~~~~

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********@gmai l.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********@gmai l.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
2405
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 relevant announcements. If you're interested, drop me a note. But if for some reason you think that this discussion is fine here at the c.l.py, please let me know. ** LONG POST AHEAD **
10
4882
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 the class is used in a windows program and has an HWND member ( a handle to a window), and to make an sense the class must know what the member is. But this puts me in the position of having no default constructor, and I've heard that is...
2
7674
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 = (data_type**)malloc(widht*height*sizeof(data_type)+ height* sizeof(data_type*)); //allocate individual addresses for row pointers. Now that I am moving to C++,am looking for something by which I can
23
5186
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
1958
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 understand that this is a rule in the language, but what is the motivation of this rule?
8
2986
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 or the default constructor is automatically called instead" Why cant the compiler do this on its own. if we are making an object through copr construction for an inherited class , then why not simply call the corresponding copy constructors for...
7
1532
by: AndreasW | last post by:
Hi, internal static class Program { public class foo { private int _id; public foo() {
3
2201
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 I've learnt that this does *not* actually call the default constructor (which leads me to the main question later on..) ..but then, what exactly is n? Seems like an uninitialized reference to NoCC...
7
2714
by: dragoncoder | last post by:
Hello experts, I have the following code me. =cat mystring.h #include<iostream> using namespace std; class mystring {
12
7222
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? class Trial { public: Trial() {
0
9721
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10635
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10115
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9198
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7653
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3013
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.