473,804 Members | 5,054 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor call constructor

Hello,

I wan't to have optionnal parameter in my cronstructor, so, I define 2
constructors.

when I call the constructor without the "optional" parameter, I want to call
the constructor with complete parameteers list.

Like this : (but it's no good)

public class SystemEvent

{

public SystemEvent(Dat eTime p_Heure, String p_Porte, String p_TypeEvt)

{

SystemEvent(p_H eure, p_Porte, p_TypeEvt,false ,true);

}

public SystemEvent(Dat eTime p_Heure,String p_Porte,String p_TypeEvt,bool
p_FromLog,bool p_WithVerifEven t)

{

.....

}
Apr 4 '06 #1
7 5605
It's not working.
You must create a private methode with the total parameter list and call
this methos from all constructors.

Mihaly
"Geoffrey" wrote:
Hello,

I wan't to have optionnal parameter in my cronstructor, so, I define 2
constructors.

when I call the constructor without the "optional" parameter, I want to call
the constructor with complete parameteers list.

Like this : (but it's no good)

public class SystemEvent

{

public SystemEvent(Dat eTime p_Heure, String p_Porte, String p_TypeEvt)

{

SystemEvent(p_H eure, p_Porte, p_TypeEvt,false ,true);

}

public SystemEvent(Dat eTime p_Heure,String p_Porte,String p_TypeEvt,bool
p_FromLog,bool p_WithVerifEven t)

{

.....

}

Apr 4 '06 #2
You can do it either way round with constructor chaining:
public class SystemEvent
{

public SystemEvent(Dat eTime p_Heure, String p_Porte, String p_TypeEvt)
{
this.p_Heure = pHeure;
this.p_Porte = p_Porte;
this.p_TypeEvt = p_TypeEvt;
}

public SystemEvent(Dat eTime p_Heure,String p_Porte,String p_TypeEvt,
bool p_FromLog,bool p_WithVerifEven t) : this (p_Heure, p_Porte,
p_TypeEvt)
{
this.p_FromLog = p_FromLog;
this.p_WithVeri fEvent;
}

}
Or you can do it the way round you have it, with the constructor with the
smaller parameter list calling the one with the larger, with default values.
This generates efficient code; calling common methods doesn't.

Andrew


Apr 4 '06 #3
Ok, thx
"amaca" <pu****@ajam.ne t> a écrit dans le message de
news:uG******** ******@TK2MSFTN GP09.phx.gbl...
You can do it either way round with constructor chaining:
public class SystemEvent
{

public SystemEvent(Dat eTime p_Heure, String p_Porte, String p_TypeEvt)
{
this.p_Heure = pHeure;
this.p_Porte = p_Porte;
this.p_TypeEvt = p_TypeEvt;
}

public SystemEvent(Dat eTime p_Heure,String p_Porte,String p_TypeEvt,
bool p_FromLog,bool p_WithVerifEven t) : this (p_Heure, p_Porte,
p_TypeEvt)
{
this.p_FromLog = p_FromLog;
this.p_WithVeri fEvent;
}

}
Or you can do it the way round you have it, with the constructor with the
smaller parameter list calling the one with the larger, with default values. This generates efficient code; calling common methods doesn't.

Andrew

Apr 4 '06 #4
Hi,

IMO it's better to concentrate all the code in only one constructor,
otherwise you may get errors as you include some code in one variant and not
the other, cause remember that you usually do more than just assign values
to variables.

In your example, what happens with "FromLog " if the first constructor is
called?
because of that yours contructor with partial parameters should call the
constructor with all the parameters and there you do your init:

public SystemEvent(Dat eTime p_Heure, String p_Porte, String p_TypeEvt) :
this (p_Heure, p_Porte, p_TypeEvt, false, false)
{}

public SystemEvent(Dat eTime p_Heure,String p_Porte,String p_TypeEvt,
bool p_FromLog,bool p_WithVerifEven t) {
this.p_FromLog = p_FromLog;
this.p_WithVeri fEvent;
this.p_Heure = pHeure;
this.p_Porte = p_Porte;
this.p_TypeEvt = p_TypeEvt;
}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"amaca" <pu****@ajam.ne t> wrote in message
news:uG******** ******@TK2MSFTN GP09.phx.gbl...
You can do it either way round with constructor chaining:
public class SystemEvent
{

public SystemEvent(Dat eTime p_Heure, String p_Porte, String p_TypeEvt)
{
this.p_Heure = pHeure;
this.p_Porte = p_Porte;
this.p_TypeEvt = p_TypeEvt;
}

public SystemEvent(Dat eTime p_Heure,String p_Porte,String p_TypeEvt,
bool p_FromLog,bool p_WithVerifEven t) : this (p_Heure, p_Porte,
p_TypeEvt)
{
this.p_FromLog = p_FromLog;
this.p_WithVeri fEvent;
}

}
Or you can do it the way round you have it, with the constructor with the
smaller parameter list calling the one with the larger, with default
values. This generates efficient code; calling common methods doesn't.

Andrew

Apr 4 '06 #5
Geoffrey wrote:
I wan't to have optionnal parameter in my cronstructor, so, I define 2
constructors.

when I call the constructor without the "optional" parameter, I want to call
the constructor with complete parameteers list.

Like this : (but it's no good)

public class SystemEvent

{

public SystemEvent(Dat eTime p_Heure, String p_Porte, String p_TypeEvt)

{

SystemEvent(p_H eure, p_Porte, p_TypeEvt,false ,true);

}

public SystemEvent(Dat eTime p_Heure,String p_Porte,String p_TypeEvt,bool
p_FromLog,bool p_WithVerifEven t)

{

....

}


Geoffrey,

Try:

public class SystemEvent
{
public SystemEvent(Dat eTime p_Heure, String p_Porte, String p_TypeEvt) :
this(p_Heure, p_Porte, p_TypeEvt, false, true)
{
}

public SystemEvent(Dat eTime p_Heure,String p_Porte,String p_TypeEvt,
bool p_FromLog,bool p_WithVerifEven t)
{
// your code here . . .
}
}
HTH,
-rick-
Apr 4 '06 #6
Personnaly, I choose to place all the code in the constructor with all the
parameters.

The constructors with minder parameters are only "shortcuts" for the full
constructor
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.mach in AT dot.state.fl.us > a
écrit dans le message de news:eu******** *******@TK2MSFT NGP12.phx.gbl.. .
Hi,

IMO it's better to concentrate all the code in only one constructor,
otherwise you may get errors as you include some code in one variant and not the other, cause remember that you usually do more than just assign values
to variables.

In your example, what happens with "FromLog " if the first constructor is
called?
because of that yours contructor with partial parameters should call the
constructor with all the parameters and there you do your init:

public SystemEvent(Dat eTime p_Heure, String p_Porte, String p_TypeEvt) : this (p_Heure, p_Porte, p_TypeEvt, false, false)
{}

public SystemEvent(Dat eTime p_Heure,String p_Porte,String p_TypeEvt,
bool p_FromLog,bool p_WithVerifEven t) {
this.p_FromLog = p_FromLog;
this.p_WithVeri fEvent;
this.p_Heure = pHeure;
this.p_Porte = p_Porte;
this.p_TypeEvt = p_TypeEvt;
}

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"amaca" <pu****@ajam.ne t> wrote in message
news:uG******** ******@TK2MSFTN GP09.phx.gbl...
You can do it either way round with constructor chaining:
public class SystemEvent
{

public SystemEvent(Dat eTime p_Heure, String p_Porte, String p_TypeEvt) {
this.p_Heure = pHeure;
this.p_Porte = p_Porte;
this.p_TypeEvt = p_TypeEvt;
}

public SystemEvent(Dat eTime p_Heure,String p_Porte,String p_TypeEvt,
bool p_FromLog,bool p_WithVerifEven t) : this (p_Heure, p_Porte,
p_TypeEvt)
{
this.p_FromLog = p_FromLog;
this.p_WithVeri fEvent;
}

}
Or you can do it the way round you have it, with the constructor with the smaller parameter list calling the one with the larger, with default
values. This generates efficient code; calling common methods doesn't.

Andrew


Apr 4 '06 #7
Geoffrey wrote:
Personnaly, I choose to place all the code in the constructor with all the parameters.

The constructors with minder parameters are only "shortcuts" for the full constructor


I agree. I have tried both ways and I find it much clearer putting all
of the code in the constructor with the most parameters and then
setting up the other constructors as "shortcuts" to it.

The only problem comes when you introduce inheritance where the child
class also has the same "shortcut" scheme. It's a toss-up as to whether
the child class should do the same as the parent, duplicating the
default values for the optional arguments, or call the parent's
"shortcut" constructors, thus duplicating the code that initializes
fields in the child.

I still haven't figured out a clean way to handle that one. Any takers?

Apr 4 '06 #8

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

Similar topics

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) .
24
3787
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
74
16041
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
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() {
9
23777
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number of objects using the given constructor. The objects should all inherit from a base class. It's not possible to pass actual objects, since it's not given on beforehand, how many should be created.
0
9706
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
9579
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
10577
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...
1
10320
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10077
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
9150
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
7620
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
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
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.