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? 12 2702
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?
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?
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
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?
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
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
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?"
"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.
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
}
};
|
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 {
|
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
|
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?
|
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 member "is a private". I believe there's more to it than
just simply that...
Theory: You inherit,...
| |
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;
|
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?
|
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();
|
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, culture, InValue);
|
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...
|
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,...
| |
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...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |