473,326 Members | 2,108 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,326 software developers and data experts.

virtual function

Hello,
I have a function in base class and a lot of child classes of it. Couple
of them need an input argument in this function and all the other don't.
How can I do it using ovveride and virtual?
Thank you!

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
7 3655
"juli jul" <ju******@yahoo.com> wrote in message
news:u0***************@TK2MSFTNGP09.phx.gbl...
Hello,
I have a function in base class and a lot of child classes of it. Couple
of them need an input argument in this function and all the other don't.
How can I do it using ovveride and virtual?
Thank you!


The problem is that they are really two different methods, the compiler
can't treat them the same as the callstack is different - even if they have
the same intent.

You could use overloading and virtuals to give a form of this

class Foo
{
public virtual void Bar()
{
Bar(0);
}
public virtual void Bar(int x)
{
}
}

class Baz : Foo
{
public override void Bar()
{
}
}

class Quux : Foo
{
public override void Bar( int x )
{
}
}

The key thing is how does the base class know whether to call the version
with no params or the one with params?
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Nov 17 '05 #2
It depends on whether you want to be able to call both versions of the
overload from a base class reference or just the one without parameters. In
either case it's not difficult

Case 1:
public class Foo
{
public virtual void Bar() {...}
}
public class Foo2 : Foo
{
public override void Bar() {...}
public void Bar(int i) {...}
}

In this case we could call Bar() from a reference to Foo or Foo2 but I could
only call Bar(int i) from a reference to Foo2. This is the most common
scenario because you normally won't know what paramater(s) the overload in
the derived class will need when declaring the base class.

Case 2:
public class Foo
{
public virtual void Bar() {...}
public virtual void Bar(int i) {...}
}
public class Foo2 : Foo
{
public override void Bar() {...}
}
public class Foo3 : Foo
{
public override void Bar(int i) {...}
}
public class Foo4 : Foo
{
public override void Bar() {...}
public override void Bar(int i) {...}
}

In this case we declare both overloaded methods in the base class, then the
derived classes can choose to override 0, 1 or both of the methods.

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada
http://msmvps.com/windsor/

"juli jul" <ju******@yahoo.com> wrote in message
news:u0***************@TK2MSFTNGP09.phx.gbl...
Hello,
I have a function in base class and a lot of child classes of it. Couple
of them need an input argument in this function and all the other don't.
How can I do it using ovveride and virtual?
Thank you!

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #3
Richard Blewett wrote:
[...snip...]
The key thing is how does the base class know whether to call the version
with no params or the one with params?

[...snip...]

Why should the base class know that ? The user of the subclass should be
responsible of knowing about the instance's class and act accordingly.
Nov 17 '05 #4
"Michael Voss" <mi********************@lvrREMOVE.CAPSde> wrote in message
news:4360ba54$1@news...
Richard Blewett wrote:
[...snip...]
The key thing is how does the base class know whether to call the version
with no params or the one with params?

[...snip...]

Why should the base class know that ? The user of the subclass should be
responsible of knowing about the instance's class and act accordingly.


Yep, point taken - thats what comes of writing in haste. I guess the
question should be who decides what version of the method should be
invoked - should all derived classes be capable of having both versions of
the method called? Or should each have only one or the other? Or should the
one with the parameterized version also support the parameterless version
but not the other way round?

Without extra information exactly how to model it is not clear.
Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Nov 17 '05 #5


The problem is that I want that the function will be defined in base
class for most classes (and this is problematic because if I will
override I will need to declare it again in derived classes and I don't
want to)
How can I make it for example this way:

For Foo2,Foo3,Foo4 I want the function to be as it is in base class with
no override and for Foo5,Foo6 I want it to get some argument and to act
as the one in the base class.

How can I do this?
Thabk you very much!

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6
"juli jul" <ju******@yahoo.com> wrote in message
news:OR**************@TK2MSFTNGP12.phx.gbl...


The problem is that I want that the function will be defined in base
class for most classes (and this is problematic because if I will
override I will need to declare it again in derived classes and I don't
want to)
How can I make it for example this way:

For Foo2,Foo3,Foo4 I want the function to be as it is in base class with
no override and for Foo5,Foo6 I want it to get some argument and to act
as the one in the base class.

How can I do this?
Thabk you very much!


How can it act as the base method if it takes a parameter?

You mean functionally it does the same thing as the base version with some
slight alteration based on the parameter?

Can the parameter version do its work before or after the base version has
run or does it affect what the base version itself would do?

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
Nov 17 '05 #7
How about...

class Base
{
private int i;
public void Test()
{
_Test(i);
}
protected void _Test(int i)
{
System.Console.WriteLine("Base "+i);
}
public void TestDefer()
{
YourDefer(i);
}
public virtual void YourDefer(int i)
{
System.Console.WriteLine("Base Defer "+i);
}
}
class Derived : Base
{
public void Test(int i)
{
i += 1;
_Test(i);
}
public override void YourDefer(int i)
{
i += 1;
System.Console.WriteLine("Derived Defer "+i);
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Derived d= new Derived();
d.Test();
d.Test(1);
d.TestDefer();
System.Console.ReadLine();
}
}

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #8

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

Similar topics

3
by: Roy Yao | last post by:
Hello, I need to pass a pointer to a callback function to the lower level modules. But the function is thought to be a virtual member one. How can I get the real address of the virtual...
23
by: heted7 | last post by:
Hi, Most of the books on C++ say something like this: "A virtual destructor should be defined if the class contains at least one virtual member function." My question is: why is it only for...
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...
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
7
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it...
17
by: Jess | last post by:
Hello, If I have a class that has virtual but non-pure declarations, like class A{ virtual void f(); }; Then is A still an abstract class? Do I have to have "virtual void f() = 0;"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.