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

Want to take the path of specific overloaded constructors from derived class up to just one below the base class.


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 app with;

D m_D = new D(tkn);

public class A : MarshalByRefObject
public A () <---------------------

public class B : A
public B () <----------------------
public B (Token tkn) : this()
{
CODE I WANT TO EXECUTE BASED ON THE VALUE OF THE TOKEN THAT WAS PASSED IN WHEN D BELOW WAS INSTANTIATED.
THROW AN EXCEPTION IF VALUE OF THE TOKEN IS eg. > 0
}


public class C : B
public C () <---------------------
public C (Token tkn) : base(tkn)
public class D: C , Interface_D
public D()
public D (Token tkn) <----------------------


I inherited this design and don't know what the base(tkn) and this() are doing? (ie. i am clueless)

What I want to happen is for the callee to begin at the bottom of the inheritance chain and call only the constructors
that have the 'Token' signature. I want this process of walking up the inheritance chain to stop once it finds the ctor at
public Class B with the Token signature.

Right now the execution path moves correctly into the lowermost class with the ctor that contains the Token method signature.
After that it jumps right up through the null constructors and never visits any of the ctors with the (Token tkn) signature.
How do I achieve this ability for the method call to get passed up the ctor chain visiting only the ctors with the Token method
and stopping at Class B before its retreat back down the stack?

Thank you... -greg


Nov 16 '05 #1
3 1574
Hopefully explaining what this() and base() mean will clarify what's happening. When you have 2 constructors like the following:

public B() { ... }
public B(Token tkn) : this() { ... }

what that means is the second constructor will first invoke the zero-argument constructor for the class and then execute it's own code. Thus, if they were

public B() { this.a = 1; }
public B(Token tkn) : this() { this.b = 2; }

the second constructor is equivalent to:

public B(Token tkn) { this.a = 1; this.b = 2; }

Putting the repeated code in the first contructor promotes code reuse.

Now, whenever you construct a class that's a derived class, a constructor on the base class MUST be invoked. By default, it invokes the zero-argument contructor, assuming it's defined and not private. However, your derived class can cause a different constructor on the base class to be called by using the base() syntax. For example, in the class C, you have

public C(Token tkn) : base(tkn)

That tells the runtime to invoke the base class contructor that takes a Token argument (i.e. B(Token tkn)). Also, keep in mind that the base class constructor is always called BEFORE the derived class's constructor.

So, the full path of your constructor when you invoke "new D(tkn)" is:

A();
B();
C();
D(Token);

However, if you change your definition of the second constructor for D to be:

public D(Token tkn) : base(tkn)

then your constructor sequence would be:

A();
B();
B(Token);
C(Token);
D(Token);

which is probably more what you're looking for.

Ken
"hazz" <ha**@sonic.net> wrote in message news:uu**************@TK2MSFTNGP10.phx.gbl...

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 app with;

D m_D = new D(tkn);

public class A : MarshalByRefObject
public A () <---------------------

public class B : A
public B () <----------------------
public B (Token tkn) : this()
{
CODE I WANT TO EXECUTE BASED ON THE VALUE OF THE TOKEN THAT WAS PASSED IN WHEN D BELOW WAS INSTANTIATED.
THROW AN EXCEPTION IF VALUE OF THE TOKEN IS eg. > 0
}


public class C : B
public C () <---------------------
public C (Token tkn) : base(tkn)
public class D: C , Interface_D
public D()
public D (Token tkn) <----------------------
I inherited this design and don't know what the base(tkn) and this() are doing? (ie. i am clueless)

What I want to happen is for the callee to begin at the bottom of the inheritance chain and call only the constructors
that have the 'Token' signature. I want this process of walking up the inheritance chain to stop once it finds the ctor at
public Class B with the Token signature.

Right now the execution path moves correctly into the lowermost class with the ctor that contains the Token method signature.
After that it jumps right up through the null constructors and never visits any of the ctors with the (Token tkn) signature.
How do I achieve this ability for the method call to get passed up the ctor chain visiting only the ctors with the Token method
and stopping at Class B before its retreat back down the stack?

Thank you... -greg


Nov 16 '05 #2
Super explanation Ken. Thank you ! I get that. After walking through your very clear explanation, I can't wait to swing by work this weekend and try that out. I'll let you know how it turns out.

Appreciatively,
-Greg
"Ken Kolda" <ke*******@elliemae-nospamplease.com> wrote in message news:uT**************@TK2MSFTNGP11.phx.gbl...
Hopefully explaining what this() and base() mean will clarify what's happening. When you have 2 constructors like the following:

public B() { ... }
public B(Token tkn) : this() { ... }

what that means is the second constructor will first invoke the zero-argument constructor for the class and then execute it's own code. Thus, if they were

public B() { this.a = 1; }
public B(Token tkn) : this() { this.b = 2; }

the second constructor is equivalent to:

public B(Token tkn) { this.a = 1; this.b = 2; }

Putting the repeated code in the first contructor promotes code reuse.

Now, whenever you construct a class that's a derived class, a constructor on the base class MUST be invoked. By default, it invokes the zero-argument contructor, assuming it's defined and not private. However, your derived class can cause a different constructor on the base class to be called by using the base() syntax. For example, in the class C, you have

public C(Token tkn) : base(tkn)

That tells the runtime to invoke the base class contructor that takes a Token argument (i.e. B(Token tkn)). Also, keep in mind that the base class constructor is always called BEFORE the derived class's constructor.

So, the full path of your constructor when you invoke "new D(tkn)" is:

A();
B();
C();
D(Token);

However, if you change your definition of the second constructor for D to be:

public D(Token tkn) : base(tkn)

then your constructor sequence would be:

A();
B();
B(Token);
C(Token);
D(Token);

which is probably more what you're looking for.

Ken
"hazz" <ha**@sonic.net> wrote in message news:uu**************@TK2MSFTNGP10.phx.gbl...

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 app with;

D m_D = new D(tkn);

public class A : MarshalByRefObject
public A () <---------------------

public class B : A
public B () <----------------------
public B (Token tkn) : this()
{
CODE I WANT TO EXECUTE BASED ON THE VALUE OF THE TOKEN THAT WAS PASSED IN WHEN D BELOW WAS INSTANTIATED.
THROW AN EXCEPTION IF VALUE OF THE TOKEN IS eg. > 0
}


public class C : B
public C () <---------------------
public C (Token tkn) : base(tkn)
public class D: C , Interface_D
public D()
public D (Token tkn) <----------------------
I inherited this design and don't know what the base(tkn) and this() are doing? (ie. i am clueless)

What I want to happen is for the callee to begin at the bottom of the inheritance chain and call only the constructors
that have the 'Token' signature. I want this process of walking up the inheritance chain to stop once it finds the ctor at
public Class B with the Token signature.

Right now the execution path moves correctly into the lowermost class with the ctor that contains the Token method signature.
After that it jumps right up through the null constructors and never visits any of the ctors with the (Token tkn) signature.
How do I achieve this ability for the method call to get passed up the ctor chain visiting only the ctors with the Token method
and stopping at Class B before its retreat back down the stack?

Thank you... -greg


Nov 16 '05 #3
Worked beautifully Ken. After making sure the dependencies on the various projects in my vs.net solution were pointing to the right places, individual components were rebuilt and the whole solution integrated together with a total solution rebuild, I am doing exactly what I want to now in traversing the object hierarchy vis a vis the overloaded ctors.
Your absolutely excellent development of the problem and solution gave me such a good fundamental understanding that I didn't give up on early attempts when things weren't working. The objects through which the execution path traverses where in different projects/components/namespaces and once those where all integrated, it was such a joy to step through the code EXACTLY the way I had envisioned it. Only because of your assistance and help.

Thank you so much for taking the time.

Appreciatively,
-Greg Hazzard

"Ken Kolda" <ke*******@elliemae-nospamplease.com> wrote in message news:uT**************@TK2MSFTNGP11.phx.gbl...
Hopefully explaining what this() and base() mean will clarify what's happening. When you have 2 constructors like the following:

public B() { ... }
public B(Token tkn) : this() { ... }

what that means is the second constructor will first invoke the zero-argument constructor for the class and then execute it's own code. Thus, if they were

public B() { this.a = 1; }
public B(Token tkn) : this() { this.b = 2; }

the second constructor is equivalent to:

public B(Token tkn) { this.a = 1; this.b = 2; }

Putting the repeated code in the first contructor promotes code reuse.

Now, whenever you construct a class that's a derived class, a constructor on the base class MUST be invoked. By default, it invokes the zero-argument contructor, assuming it's defined and not private. However, your derived class can cause a different constructor on the base class to be called by using the base() syntax. For example, in the class C, you have

public C(Token tkn) : base(tkn)

That tells the runtime to invoke the base class contructor that takes a Token argument (i.e. B(Token tkn)). Also, keep in mind that the base class constructor is always called BEFORE the derived class's constructor.

So, the full path of your constructor when you invoke "new D(tkn)" is:

A();
B();
C();
D(Token);

However, if you change your definition of the second constructor for D to be:

public D(Token tkn) : base(tkn)

then your constructor sequence would be:

A();
B();
B(Token);
C(Token);
D(Token);

which is probably more what you're looking for.

Ken
"hazz" <ha**@sonic.net> wrote in message news:uu**************@TK2MSFTNGP10.phx.gbl...

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 app with;

D m_D = new D(tkn);

public class A : MarshalByRefObject
public A () <---------------------

public class B : A
public B () <----------------------
public B (Token tkn) : this()
{
CODE I WANT TO EXECUTE BASED ON THE VALUE OF THE TOKEN THAT WAS PASSED IN WHEN D BELOW WAS INSTANTIATED.
THROW AN EXCEPTION IF VALUE OF THE TOKEN IS eg. > 0
}


public class C : B
public C () <---------------------
public C (Token tkn) : base(tkn)
public class D: C , Interface_D
public D()
public D (Token tkn) <----------------------
I inherited this design and don't know what the base(tkn) and this() are doing? (ie. i am clueless)

What I want to happen is for the callee to begin at the bottom of the inheritance chain and call only the constructors
that have the 'Token' signature. I want this process of walking up the inheritance chain to stop once it finds the ctor at
public Class B with the Token signature.

Right now the execution path moves correctly into the lowermost class with the ctor that contains the Token method signature.
After that it jumps right up through the null constructors and never visits any of the ctors with the (Token tkn) signature.
How do I achieve this ability for the method call to get passed up the ctor chain visiting only the ctors with the Token method
and stopping at Class B before its retreat back down the stack?

Thank you... -greg


Nov 16 '05 #4

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
3
by: SLE | last post by:
Hi there, I know constructors are not inherited from the base class, not in VB.NET nor C# (nor Java I suppose). I never wondered,but reflecting on the reason why, I cannot find a solid answer. ...
4
by: Xavier | last post by:
Hi, I have a question, in a "dreaded diamond" situation, regarding the following code: ---- Begin code #include <iostream> using namespace std;
13
by: olanglois | last post by:
Hi, I am trying to derive a new class that will add new functions but no new data members and the base class has overloaded operators (+,-,+=,-=,etc...) returning either (Base &) or (const Base)...
39
by: Digital Puer | last post by:
I'm not the world's greatest C++ programmer, so I had a hard time with these. Some help would be appreciated. 1. Comment on the declaration of function Bar() below: class Foo { static int...
4
by: nozyrev | last post by:
Hi, I apologize in advance if this is a very dumb question. I've been struggling with this problem for some time: I have an abstract base class called Base and n derived classes D1, D2, ....Dn. I...
10
by: ayan4u | last post by:
suppose i have... class base { public: base(); base( char*);
9
by: Anthony Williams | last post by:
Hi, Should the following compile, and what should it print? #include <memory> #include <iostream> void foo(std::auto_ptr<intx) { std::cout<<"copy"<<std::endl;
6
by: Joe HM | last post by:
Hello - I have a question regarding overloading the New() in a MustInherit Class. The following show the code in question ... MustInherit Class cAbstract Public MustOverride Sub print() ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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
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...

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.