473,657 Members | 2,409 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 : MarshalByRefObj ect
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 1588
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******** ******@TK2MSFTN GP10.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 : MarshalByRefObj ect
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*******@elli emae-nospamplease.co m> wrote in message news:uT******** ******@TK2MSFTN GP11.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******** ******@TK2MSFTN GP10.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 : MarshalByRefObj ect
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*******@elli emae-nospamplease.co m> wrote in message news:uT******** ******@TK2MSFTN GP11.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******** ******@TK2MSFTN GP10.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 : MarshalByRefObj ect
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
5757
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
3
1764
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. Is the reason technical (compiler or CLR limitation) or logical (OOP best practices)? Any feedback would be greatly appreciated.
4
4611
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
1992
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) depending on the operator: class Derived : public Base { };
39
2504
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 Bar(int i) const; } (I said that since Bar is a static method, it can only access
4
1668
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 would like to have a constructor for each derived class that takes any of the other derived classes as an argument so that these statements are valid: D1 d1; D2 d2;
10
1511
by: ayan4u | last post by:
suppose i have... class base { public: base(); base( char*);
9
2308
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
4165
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() Protected mName As String
0
8395
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
8310
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
8826
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
8732
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
8605
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
7330
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...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.