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

virtual function question

Hi,

Ones more I have a stupid virtual function question. Assume the
following simple lines of code

class Parent {
public:
virtual Parent foo(Parent &);
virtual void bar(Parent &);
}

class Child : public Parent {
Child foo(Child &);
void bar(Child &);
}

Will foo and bar be virtually overloaded?
Or is this here a complete misconception of virtual functions?

Thanks
Jan 18 '08 #1
2 1285
Okay sorry for the waste of internet bits ... a simple test gave me the
answer.

#include <iostream>

class P {
public:
virtual P foo(P &a) { std::cout << "P::foo called" <<
std::endl; return a; }
virtual void bar(P &a) { std::cout << "P::bar called" <<
std::endl; }
virtual void print(void) {std::cout << "P::print called" <<
std::endl; }
};

class C : public P {
public:
C foo(C &a) { std::cout << "C::foo called" << std::endl;
return a; }
void bar(C &a) { std::cout << "C::bar called" << std::endl; }
void print(void) {std::cout << "C::print called" << std::endl; }
};

int main(void) {
P a, *ptr;
C c;

a.foo(a);
a.bar(a);
c.foo(c);
c.bar(c);
a.print();
c.print();

ptr = &c;
ptr->foo(c);
ptr->foo(a);
ptr->bar(c);
ptr->bar(a);
ptr->print();
};

OUTPUT
P::foo called
P::bar called
C::foo called
C::bar called
P::print called
C::print called
P::foo called
P::foo called
P::bar called
P::bar called
C::print called
Jan 18 '08 #2
ciccio wrote:
=
>
Will foo and bar be virtually overloaded?
Or is this here a complete misconception of virtual functions?
The term is OVERRIDING. Overloading refers to multiple definitions
of the same name, for example:
Parent::foo(int);
Parent::foo(long);
would be OVERLOADS.

And no you don't have overriding. C::foo(C&) doesn't override
P::foo(P&).

The only wiggle room you get is in the return type. The following
is allowed:

struct P {
P& foo(P&) { return *this; }
};

struct C : P {
C& foo(P&) { return *this; }
};

In this case C::foo overrides the same signature function in
main. The return type is allowed to be a pointer or reference
to the derived type and still work.
Jan 18 '08 #3

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

Similar topics

4
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived...
8
by: Wouter Moors | last post by:
Hi all, i got a question about virtual functions. Suppose class A has a virtual function doIt() and in my derived class B i do not declare it as virtual. Now, will that function still be...
15
by: Dave Townsend | last post by:
Yo, I had a job interview today, the interviewing asked me about inline virtual functions, or what was my opinion on them. Hm, I've seen mention of these babies in the reference material, but...
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
5
by: gouqizi.lvcha | last post by:
Hi, all: I have 3 class X, Y, Z class Y is a subclass of class X; class Z is a subclass of class Y; i.e. class Y : public class X class Z : public class Y
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
3
by: Jimmy | last post by:
I was browsing the C++ FAQ Lite the other day when I came accross question #23.4 (http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.4). The question was "When should someone...
7
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
4
by: cwc5w | last post by:
I have two classes. One with a regular destructor and the other with a virtual destructor. e.g. class x { ~x(){} } vs
7
by: Markus Svilans | last post by:
Hello, My question involves virtual functions and inheritance. Suppose we have a class structure, that consists of "data" classes, and "processor" classes. The data classes are derived from...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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...
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...

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.