473,549 Members | 2,874 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Call root base method from doubly derived class?

Given

public class A {
virtual void foo() { ... }
}

public class B : A {
public override void foo() { ... }
public Bthingy() { ... base.foo() ...}
}

public class C : B {
public override void foo() { . . . }
public Cthingy() { ... base.foo() ... }
}
Bthingy's 'base.foo()' calls class A's foo.
Cthingy's 'base.foo()' calls class B's foo.

Is there a way for Cthingy to call the class A foo?

Dec 15 '05 #1
12 2688
DrBonzo,

No, there isn't. B would have to expose a method which would expose the
base method of A, without doing anything else.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"DrBonzo" <Dr*****@discus sions.microsoft .com> wrote in message
news:73******** *************** ***********@mic rosoft.com...
Given

public class A {
virtual void foo() { ... }
}

public class B : A {
public override void foo() { ... }
public Bthingy() { ... base.foo() ...}
}

public class C : B {
public override void foo() { . . . }
public Cthingy() { ... base.foo() ... }
}
Bthingy's 'base.foo()' calls class A's foo.
Cthingy's 'base.foo()' calls class B's foo.

Is there a way for Cthingy to call the class A foo?

Dec 15 '05 #2
No.

"DrBonzo" <Dr*****@discus sions.microsoft .com> wrote in message
news:73******** *************** ***********@mic rosoft.com...
Given

public class A {
virtual void foo() { ... }
}

public class B : A {
public override void foo() { ... }
public Bthingy() { ... base.foo() ...}
}

public class C : B {
public override void foo() { . . . }
public Cthingy() { ... base.foo() ... }
}
Bthingy's 'base.foo()' calls class A's foo.
Cthingy's 'base.foo()' calls class B's foo.

Is there a way for Cthingy to call the class A foo?

Dec 15 '05 #3

DrBonzo wrote:
Given

public class A {
virtual void foo() { ... }
}

public class B : A {
public override void foo() { ... }
public Bthingy() { ... base.foo() ...}
}

public class C : B {
public override void foo() { . . . }
public Cthingy() { ... base.foo() ... }
}
Bthingy's 'base.foo()' calls class A's foo.
Cthingy's 'base.foo()' calls class B's foo.

Is there a way for Cthingy to call the class A foo?


Directly? No. Indirectly? Sure.

public Cthingy()
{
A a = new A();
// Copy the parts of the C you want into the A
a.foo();
// Copy the parts back into C from the A.
}

Okay, yes, its dumb. But you can do it :)

Matt

Dec 15 '05 #4
Hi,

Nop, none

To C its parent is B + A + ... + Object; there is no distinction of where
a method was declared or how many times it was overriden. That's why it
access throug base and not with the class name.
cheers,

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

"DrBonzo" <Dr*****@discus sions.microsoft .com> wrote in message
news:73******** *************** ***********@mic rosoft.com...
Given

public class A {
virtual void foo() { ... }
}

public class B : A {
public override void foo() { ... }
public Bthingy() { ... base.foo() ...}
}

public class C : B {
public override void foo() { . . . }
public Cthingy() { ... base.foo() ... }
}
Bthingy's 'base.foo()' calls class A's foo.
Cthingy's 'base.foo()' calls class B's foo.

Is there a way for Cthingy to call the class A foo?

Dec 15 '05 #5
Hi,
public Cthingy()
{
A a = new A();
// Copy the parts of the C you want into the A
a.foo();
// Copy the parts back into C from the A.
}

Okay, yes, its dumb. But you can do it :)

This works in the case that A.foo is stateless ( do not change any member
variable) or if any changed member is accesible from C , if foo() change a
private member it will not work.

IMO this is a completely different situation that the one presented by the
OP. A derived class should not worry who of the ancesors override a method,
not should need to call a particular overriden method in the hierarchic
tree.

I would assume a bad design if this is the case.

any comments from the NG?
cheers,

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

Dec 15 '05 #6

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,
public Cthingy()
{
A a = new A();
// Copy the parts of the C you want into the A
a.foo();
// Copy the parts back into C from the A.
}

Okay, yes, its dumb. But you can do it :)

This works in the case that A.foo is stateless ( do not change any member
variable) or if any changed member is accesible from C , if foo() change a
private member it will not work.

IMO this is a completely different situation that the one presented by the
OP. A derived class should not worry who of the ancesors override a method,
not should need to call a particular overriden method in the hierarchic
tree.

I would assume a bad design if this is the case.

any comments from the NG?


Oh, I agree with you. I was just saying you CAN call the A::foo method
from
C. Its ugly, and it may or may not do what you want (you could easily
create a
method in A to deal with the state issues), but its really not OO at
all.

Matt

Dec 15 '05 #7
There was a discussion in this newsgroup over a year ago involving a
rather determined fellow who _swore_ that he had to do this. He decided
to write a tool that would alter the IL written by the compiler. I
don't know how that turned out (and I don't want to know--it was a
horrible idea).

The consensus at the time was that how you handle this situation
depends upon what it is you're trying to do, so with class names A, B,
and C, it's hard to say.

Typically, what's going on is that A's "foo" method (or property)
serves more than one purpose. The derived class B then overrides "foo"
in order to modify its behaviour, but in so doing makes it useful in
only one of the two possible contexts in which it can be used. C then
wants to call B's foo in one context, but in another context wants the
"other" way of using A's "foo", which B doesn't provide.

The solution is, as several here have pointed out, to create a new
method / property in B that exposes the functionality of "foo" for use
in that other context.

Now, the reason for all of the fancy language is that this all has to
do with semantics and design. What is "foo", exactly, and what is it
for? If it has two purposes, how would you name those? Let me give you
a concrete example. It may not be the best example going, but it serves
to illustrate the concept:

public class StockItem
{
public virtual decimal Price { get { ... }}
}

public class ImportedStockIt em : StockItem
{
public override decimal Price { get { ... include import duties,
etc. ... }}
}

public class ImportedStockIt emForInternalUs e : ImportedStockIt em
{
...
}

Yeah, I know: it's a stupid class hierarchy, but it's just for
illustration. What if, in class ImportedStockIt emForInternalUs e, you
needed to know the item's price before import duties, taxes, and other
stuff that was added in by the base class ImportedStockIt em? How do you
get that?

You get that by realizing that StockItem.Price can be used for two
things in ImportedStockIt emForInternalUs e, and changing that class
accordingly:

public class ImportedStockIt em : StockItem
{
public override decimal Price { get { ... include import duties,
etc. ... }}
public decimal PriceBeforeDuty AndTaxes { get { return base.Price;
}}
}

The point is that you expose base.Price as a second property (or
method), but with a name that explains why you're doing that, not with
some lame name like "BaseClassPrice ". It's all about asking, "What does
this information mean, and why do I need it?"

Dec 15 '05 #8

"DrBonzo" <Dr*****@discus sions.microsoft .com> wrote in message
news:73******** *************** ***********@mic rosoft.com...
Given

public class A {
virtual void foo() { ... }
}

public class B : A {
public override void foo() { ... }
public Bthingy() { ... base.foo() ...}
}

public class C : B {
public override void foo() { . . . }
public Cthingy() { ... base.foo() ... }
}
Bthingy's 'base.foo()' calls class A's foo.
Cthingy's 'base.foo()' calls class B's foo.

Is there a way for Cthingy to call the class A foo?


This is not possible in C# (which is a good thing IMO), only IL and C++/CLI
allows you to make static calls to virtual bases (provided your root base is
made non private).
Just curious, why you would need this for?

Willy.
Dec 15 '05 #9
Gee, thanks for the replies.

The answer is not unexpected, but disappointing anyway.

Interestingly, IntelliSense seems to know what I intend when I type
'base.base.',
but of course the compiler gags. Since there's only single inheritance,
this seems
like a fairly reasonable thing to want to do.

As for Willy's question, here's a silly graphical example - class A is a basic
square, say, and foo gets it drawn. Class B is a square with decorated
corners and B::foo (to mix metaphors) just draws the decoration.
Class C is a corner-decorated square with decorated center and
C::foo just draws the center decoration.

In my actual case, each class has its own additions to an XML document.

"DrBonzo" wrote:
Given

public class A {
virtual void foo() { ... }
}

public class B : A {
public override void foo() { ... }
public Bthingy() { ... base.foo() ...}
}

public class C : B {
public override void foo() { . . . }
public Cthingy() { ... base.foo() ... }
}
Bthingy's 'base.foo()' calls class A's foo.
Cthingy's 'base.foo()' calls class B's foo.

Is there a way for Cthingy to call the class A foo?

Dec 16 '05 #10

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

Similar topics

6
5626
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
9
3657
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; template<class T> classSubject {
4
6069
by: Amy Matlock | last post by:
Hi all: How does the hardware work if you invoke a BASE::METHOD() on a DERIVED class member? Do you still hit the v-table dynamically at run time? Suppose you have a derived class method, but you use the :: scoping operator to directly call the base class method. Like this: class BASE
9
3706
by: Jiho Han | last post by:
Suppose I have an xml fragment like: <mother> <child name="Bob" sex="M"/> <child name="Jane" sex="F"/> ... </mother> If I wanted to replace the <mother> element to <father> element, what is the fastest or most efficient way to do this?
7
2213
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the...
6
2475
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
6
4332
by: marcwentink | last post by:
Say I have class A that inherits from class B, and I call class A his constructor. Then somehow class B his constructor is called also. How does this work? Is a constructor under the hood a sort of virtual method by default, and are the ancestors constructors called in a sort of VT table?
2
6100
by: Kevin Frey | last post by:
In a derived class I am trying to redefine the implementation of a interface method defined in a base class - the base class interface method was not declared virtual. I have yet to actually test this approach, but it looks ugly. Is there any better way? public interface ITest { void Func();
9
5833
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to the static Parse method of the conversion class. if (InValue is string) return T.Parse((string)InValue); else return base.ConvertFrom(context,...
0
7750
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. ...
0
7991
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...
1
7509
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...
0
6084
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...
0
5118
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...
0
3524
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1971
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
1
1084
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
790
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...

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.