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

new virtual functions

I have a question about the use of the "new" and "virtual" keywords
together. Say I have the following four classes

class A {
virtual public void f() {
System.Console.WriteLine("A.f()");
}
}

class B : A {
override public void f() {
System.Console.WriteLine("B.f()");
}
}

class C : B {
new virtual public void f() {
System.Console.WriteLine("C.f()");
}
}

class D : C {
override public void f() {
System.Console.WriteLine("D.f()");
}
}

and I use them like this:

class Test {
[STAThread]
static void Main(string[] args) {
C cRef = new D();
cRef.f(); //prints out "D.f()"

A aRef = new D();
aRef.f(); //prints out "B.f()"?!
}
}

It makes sense to me that cRef.f() prints out "D.f()", that is basic
polymorphism. What doesn't make that much sense to me is why aRef.f()
prints out "B.f()". Why should a reference of type A pointing to an
object of type D call B.f()? I realize that it has to do with the fact
that f() is declared as new virtual in C. What I don't understand is
why this is the correct behavior. Why would you even want to do
something like this? How could this be used?

Thanks,
Dave

Nov 16 '05 #1
2 2492
headware <dk****@cox.net> wrote:

<snip>
It makes sense to me that cRef.f() prints out "D.f()", that is basic
polymorphism. What doesn't make that much sense to me is why aRef.f()
prints out "B.f()". Why should a reference of type A pointing to an
object of type D call B.f()? I realize that it has to do with the fact
that f() is declared as new virtual in C. What I don't understand is
why this is the correct behavior. Why would you even want to do
something like this? How could this be used?


B.f() overrides A.f(), which is basically what you're calling when you
use aRef.f(). C.f() and D.f() are completely separate methods to A.f()
and B.f() as far as the compiler is concerned.

As for why/when you'd want to use it - very rarely, basically. The
usual situation would be if you've got a library class and a class
you've derived from it. You've put in a method called Foo(), and then
in the next version of the library that class gains a method called
Foo() as well, which might be meant to do something completely
different. You don't want to override the library class's Foo() method,
because then the library class would be expecting it to do one thing
and it would actually do another, but you still want your code which
uses your Foo() method to compile and run properly.

--
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
I see. You're right, that doesn't seem like something you would use too
often, but that does solve the problem you mentioned. Thanks for the
reply.

Dave

Jon wrote:
headware <dk****@cox.net> wrote:

<snip>
It makes sense to me that cRef.f() prints out "D.f()", that is basic polymorphism. What doesn't make that much sense to me is why aRef.f() prints out "B.f()". Why should a reference of type A pointing to an
object of type D call B.f()? I realize that it has to do with the fact that f() is declared as new virtual in C. What I don't understand is why this is the correct behavior. Why would you even want to do
something like this? How could this be used?
B.f() overrides A.f(), which is basically what you're calling when

you use aRef.f(). C.f() and D.f() are completely separate methods to A.f() and B.f() as far as the compiler is concerned.

As for why/when you'd want to use it - very rarely, basically. The
usual situation would be if you've got a library class and a class
you've derived from it. You've put in a method called Foo(), and then in the next version of the library that class gains a method called
Foo() as well, which might be meant to do something completely
different. You don't want to override the library class's Foo() method, because then the library class would be expecting it to do one thing
and it would actually do another, but you still want your code which
uses your Foo() method to compile and run properly.

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


Nov 16 '05 #3

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

Similar topics

4
by: vijay | last post by:
I have a doubt with size of classed with virtual functions I have declared A,A1,A2 ,B , C, D some classes with no varaibles but a vitual function each, The size of A is as expected 4 bytes with...
5
by: Ryan Faulkner | last post by:
Hi, Im having a few problems with virtual functions (Im using the Visual C++ environment by the way). I have a base class with three virtual functions and a derived class with a single new...
18
by: nenad | last post by:
Wouldn't it be nice if we could do something like this: class Funky{ public: auto virtual void doStuff(){ // dostuff } };
19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
7
by: Aguilar, James | last post by:
I've heard that virtual functions are relatively ineffecient, especially virtual functions that are small but get called very frequently. Could someone describe for me the process by which the...
25
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use...
11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
7
by: ashishnh33 | last post by:
i want to know about the concept behind the virtual functions.
5
by: Dilip | last post by:
In a polymorphic hierarchy is it common practice to have public virtual functions in the base class with a default empty implementation and let the derived classes decide whether they want to...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
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...
0
isladogs
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...
0
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...

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.