473,405 Members | 2,261 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,405 software developers and data experts.

Constructor initializers

Assume a class DerivedClass has two constructors:

DerivedClass(int a)
{
// some code
}

DerivedClass(int a, int b) : base(a, b)
{
}
As you can see, I want the second constructor to execute code in a base
class constructor, however I also want the second constructor to execute the
code in the first constructor (in the DerivedClass),
denoted as // some code.

I can only see 2 alternatives:

Alternative 1: copy all the code in the first constructor (// some code), so
that the second constructor looks like:
DerivedClass(int a, int b) : BaseClass(a, b)
{
// some code
}
This approach may lead to maintenance problems.

Alternative 2: factorize the code to another (instance) method (should be
private) and call it from both constructors.
This alternative works.

Anyone knows a better approach ? I am looking for something like:
DerivedClass(int a, int b) : base(a, b) , this(a) (executing both a base
class constructor and a "sibling" constructor)

Nov 16 '05 #1
3 1652
The other option is to move //some code into the second constructor, and
have the first one call the second, with a default value for 'b'.

"Magne Ryholt" <ma**********@bluezone.no> wrote in message
news:OF**************@tk2msftngp13.phx.gbl...
Assume a class DerivedClass has two constructors:

DerivedClass(int a)
{
// some code
}

DerivedClass(int a, int b) : base(a, b)
{
}
As you can see, I want the second constructor to execute code in a base
class constructor, however I also want the second constructor to execute the code in the first constructor (in the DerivedClass),
denoted as // some code.

I can only see 2 alternatives:

Alternative 1: copy all the code in the first constructor (// some code), so that the second constructor looks like:
DerivedClass(int a, int b) : BaseClass(a, b)
{
// some code
}
This approach may lead to maintenance problems.

Alternative 2: factorize the code to another (instance) method (should be
private) and call it from both constructors.
This alternative works.

Anyone knows a better approach ? I am looking for something like:
DerivedClass(int a, int b) : base(a, b) , this(a) (executing both a base
class constructor and a "sibling" constructor)

Nov 16 '05 #2
Magne,

In this case, you should just have the overload with one parameter call
the overload with two parameters, like so:

DerivedClass(int a) : this(a, 0)
{}

DerivedClass(int a, int b) : base(a, b)
{}

Is there a value that can be used for b which is outside what is
expected? If so, use that. I use zero only as an example here.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Magne Ryholt" <ma**********@bluezone.no> wrote in message
news:OF**************@tk2msftngp13.phx.gbl...
Assume a class DerivedClass has two constructors:

DerivedClass(int a)
{
// some code
}

DerivedClass(int a, int b) : base(a, b)
{
}
As you can see, I want the second constructor to execute code in a base
class constructor, however I also want the second constructor to execute
the code in the first constructor (in the DerivedClass),
denoted as // some code.

I can only see 2 alternatives:

Alternative 1: copy all the code in the first constructor (// some code),
so that the second constructor looks like:
DerivedClass(int a, int b) : BaseClass(a, b)
{
// some code
}
This approach may lead to maintenance problems.

Alternative 2: factorize the code to another (instance) method (should be
private) and call it from both constructors.
This alternative works.

Anyone knows a better approach ? I am looking for something like:
DerivedClass(int a, int b) : base(a, b) , this(a) (executing both a base
class constructor and a "sibling" constructor)

Nov 16 '05 #3
Thank you,
sure I could, however I forgot to say that the first constructor (in
DerivedClass) looks like: DerivedClass(int a) : base(a)
Sorry :)

"Marina" <so*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
The other option is to move //some code into the second constructor, and
have the first one call the second, with a default value for 'b'.

"Magne Ryholt" <ma**********@bluezone.no> wrote in message
news:OF**************@tk2msftngp13.phx.gbl...
Assume a class DerivedClass has two constructors:

DerivedClass(int a)
{
// some code
}

DerivedClass(int a, int b) : base(a, b)
{
}
As you can see, I want the second constructor to execute code in a base
class constructor, however I also want the second constructor to execute

the
code in the first constructor (in the DerivedClass),
denoted as // some code.

I can only see 2 alternatives:

Alternative 1: copy all the code in the first constructor (// some code),

so
that the second constructor looks like:
DerivedClass(int a, int b) : BaseClass(a, b)
{
// some code
}
This approach may lead to maintenance problems.

Alternative 2: factorize the code to another (instance) method (should be
private) and call it from both constructors.
This alternative works.

Anyone knows a better approach ? I am looking for something like:
DerivedClass(int a, int b) : base(a, b) , this(a) (executing both a base
class constructor and a "sibling" constructor)



Nov 16 '05 #4

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

Similar topics

18
by: Makis Papapanagiotou | last post by:
Hello all, There is a strange case where a class Test is composed from three objects of other classes. i.e. class Test { public: Test(); private: Point x;
6
by: Dhananjaya.R | last post by:
Hi, What will be the constructor flow of control for the below snippet. I had an hard time understanding how C# can allow initialization of member variables in class declaration. public class...
4
by: Wilfried Mestdagh | last post by:
Hi, It seems to me (using C# 2005 beta and NET 2 beta) that both of these are the same: public class test { int t = 500; }
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
8
by: Jess | last post by:
Hello, When I define default constructors, I tend to use constructor initializers for member data. However, I was told the order in which members are initialized is determined by the order of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.