473,387 Members | 1,619 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,387 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 2491
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.