473,386 Members | 1,821 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,386 software developers and data experts.

if derived class adds an argument to ctor, does that need to carry up to the base classes?

I have a class/constructor hiearchy that functions correctly as illustrated
below (thanks to Ken Kolda's earlier newsgroup assistance.) If a client
instantiates object D, eg. D m_D = new D(str_arg), it climbs up the ctor
stack until B(string str) handles it.

A();

B();
B(string str)
{
do_string_Stuff; }

C();
C(string str) : base(str);

D();
D(string str) : base(str);

Now for the change. I have a class that has this ctor signature.
D ( long n_arg, string str) // :base(str) won't work anymore. will
it have to be :base(n_arg, str)

D m_D = new D(long n_arg2, string str_arg ) would work, just adding an
additional string argument, but I want to get up to the overloaded ctor in
B. How do I change this object design in the least invasive manner? Do I
have to add new constructors with the additional n_arg to C and B for this
to work as per the following?

A();

B();
B(string str)
{ do_string_stuff; }
B (long n_arg, string str)
{ do_string_stuff; }

C();
C(string str) : base(str);
C( long n_arg, string str) : base(n_arg, str);

D();
D(string str) : base(str);
D (long n_arg, string str) : base(n_arg, str);

Thank you very much,
-greg
Nov 16 '05 #1
6 1277
hazz <ha**@sonic.net> wrote:
I have a class/constructor hiearchy that functions correctly as
illustrated below (thanks to Ken Kolda's earlier newsgroup
assistance.) If a client instantiates object D, eg. D m_D = new
D(str_arg), it climbs up the ctor stack until B(string str) handles
it.

A();

B();
B(string str)
{
do_string_Stuff; }

C();
C(string str) : base(str);

D();
D(string str) : base(str);

Now for the change. I have a class that has this ctor signature.
D ( long n_arg, string str) // :base(str) won't work anymore. will
it have to be :base(n_arg, str)


base(str) should work absolutely fine. Why do you think it wouldn't?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

"hazz" <ha**@sonic.net> wrote in message
news:O%***************@TK2MSFTNGP12.phx.gbl...
I have a class/constructor hiearchy that functions correctly as illustrated below (thanks to Ken Kolda's earlier newsgroup assistance.) If a client
instantiates object D, eg. D m_D = new D(str_arg), it climbs up the ctor
stack until B(string str) handles it.

A();

B();
B(string str)
{
do_string_Stuff; }

C();
C(string str) : base(str);

D();
D(string str) : base(str);

Now for the change. I have a class that has this ctor signature.
D ( long n_arg, string str) // :base(str) won't work anymore. will
it have to be :base(n_arg, str)

It does has to work !
It will also work :this( str) which call the other constructor of D
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 16 '05 #3
thanks for responding Jon.
base(str) DID work absolutely fine as you correctly pointed out.
However, D (long n_arg, string str) has been added to the mix.
So should D (long n_arg, string str) : base(n_arg, str) work absolutely
fine as per my best attempt to try to explain how I 'thought' it might work
as per the psuedocode that follows immediately;

A();

B();
B(string str)
{ do_string_stuff; }
B (long n_arg, string str)
{ do_string_stuff; }

C();
C(string str) : base(str);
C( long n_arg, string str) : base(n_arg, str);

D();
D(string str) : base(str);
D (long n_arg, string str) : base(n_arg, str);

If I am missing the obvious, please let me know.

Appreciatively,
-greg

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
hazz <ha**@sonic.net> wrote:
I have a class/constructor hiearchy that functions correctly as
illustrated below (thanks to Ken Kolda's earlier newsgroup
assistance.) If a client instantiates object D, eg. D m_D = new
D(str_arg), it climbs up the ctor stack until B(string str) handles
it.

A();

B();
B(string str)
{
do_string_Stuff; }

C();
C(string str) : base(str);

D();
D(string str) : base(str);

Now for the change. I have a class that has this ctor signature.
D ( long n_arg, string str) // :base(str) won't work anymore. will it have to be :base(n_arg, str)


base(str) should work absolutely fine. Why do you think it wouldn't?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4
Thank you Ignacio. You are mentioning the 'this' keyword. Like this;

A();

B();
B(string str)
{ do_string_stuff; }

C();
C(string str) : base(str);

D();
D(string str) : base(str);
D ( long n_arg, string str) : this

D m_D = new D(n_arg, str_arg) would then instantiate properly and
additionally would pass the string up to B. Correct?

thx, -greg
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:eo**************@TK2MSFTNGP10.phx.gbl...

"hazz" <ha**@sonic.net> wrote in message
news:O%***************@TK2MSFTNGP12.phx.gbl...
I have a class/constructor hiearchy that functions correctly as

illustrated
below (thanks to Ken Kolda's earlier newsgroup assistance.) If a client
instantiates object D, eg. D m_D = new D(str_arg), it climbs up the ctor
stack until B(string str) handles it.

A();

B();
B(string str)
{
do_string_Stuff; }

C();
C(string str) : base(str);

D();
D(string str) : base(str);

Now for the change. I have a class that has this ctor signature.
D ( long n_arg, string str) // :base(str) won't work anymore. will it have to be :base(n_arg, str)

It does has to work !
It will also work :this( str) which call the other constructor of D
Cheers,

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

Nov 16 '05 #5
hazz <ha**@sonic.net> wrote:
thanks for responding Jon.
base(str) DID work absolutely fine as you correctly pointed out.
However, D (long n_arg, string str) has been added to the mix.
So should D (long n_arg, string str) : base(n_arg, str) work absolutely
fine as per my best attempt to try to explain how I 'thought' it might work
as per the psuedocode that follows immediately;


Yes, it should work fine. So long as you either explicitly call a
constructor of the base class with appropriate arguments, or call
another constructor of the current class, or don't do anything and
leave the compiler to add an implicit base() call (which obviously has
to be valid) you should be okay.

See http://www.pobox.com/~skeet/csharp/constructors.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
To be syntactically correct , > D ( long n_arg, string str) : this(str)

"hazz" <ha**@sonic.net> wrote in message
news:eh*************@TK2MSFTNGP12.phx.gbl...
Thank you Ignacio. You are mentioning the 'this' keyword. Like this;

A();

B();
B(string str)
{ do_string_stuff; }

C();
C(string str) : base(str);

D();
D(string str) : base(str);
D ( long n_arg, string str) : this

D m_D = new D(n_arg, str_arg) would then instantiate properly and
additionally would pass the string up to B. Correct?

thx, -greg
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message news:eo**************@TK2MSFTNGP10.phx.gbl...

"hazz" <ha**@sonic.net> wrote in message
news:O%***************@TK2MSFTNGP12.phx.gbl...
I have a class/constructor hiearchy that functions correctly as

illustrated
below (thanks to Ken Kolda's earlier newsgroup assistance.) If a client instantiates object D, eg. D m_D = new D(str_arg), it climbs up the ctor stack until B(string str) handles it.

A();

B();
B(string str)
{
do_string_Stuff; }

C();
C(string str) : base(str);

D();
D(string str) : base(str);

Now for the change. I have a class that has this ctor signature.
D ( long n_arg, string str) // :base(str) won't work anymore. will it have to be :base(n_arg, str)

It does has to work !
It will also work :this( str) which call the other constructor of D
Cheers,

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


Nov 16 '05 #7

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
7
by: Tron Thomas | last post by:
Under the right compiler the following code: class Base { public: virtual void Method(int){} }; class Derived: public Base {
3
by: hazz | last post by:
The following classes follow from the base class ' A ' down to the derived class ' D ' at the bottom of the inheritance chain. I am calling the class at the bottom, "public class D" from a client...
0
by: hamstak | last post by:
I had developed a custom base page class for the 1.1 framework which I have now migrated to the 2.0 framework. I discovered that the new partial class system -- which appears to "automagically"...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
10
by: campos | last post by:
"Effective C++ 3rd Edition" Item 6, P39 ------------------------------------------------------- class Uncopyable { protected: // allow construction Uncopyable() {} // and...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.