Can a virtual method be overloaded ?

B

Bit byte

I have a class BaseApplication, from which I am deriving two classes A
and B.

In class BaseApplication, I have a virtual method as ff:

virtual void saveToDb(const char*);

I will like to overload this function in A and B, because I want to have
the ff (for example)

in class A:

void saveToDb(const struct relevant_to_a_* );

and in class B :

void saveToDb(const struct relevant_to_b_* );
void saveToDb(const struct also_relevant_to_b_* );
void saveToDb(const struct yet_another_one_relevant_to_b_* );


Is this possible? (also - are there any pitfalls I should be aware of? -
 
V

Victor Bazarov

Bit said:
I have a class BaseApplication, from which I am deriving two classes A
and B.

In class BaseApplication, I have a virtual method as ff:

virtual void saveToDb(const char*);

So, you essentially have

class BaseApplication
{
public:
virtual void saveToDb(const char*);
};

, right? This is much easier to understand.
I will like to overload this function in A and B, because I want to
have the ff (for example)

What's "ff"?
in class A:

void saveToDb(const struct relevant_to_a_* );

That's not overloading. That's hiding (presuming that 'A' inherits
from 'BaseApplication' of course).
and in class B :

void saveToDb(const struct relevant_to_b_* );
void saveToDb(const struct also_relevant_to_b_* );
void saveToDb(const struct yet_another_one_relevant_to_b_* );

Those three are overloaded between themselves, but if 'B' derives from
the 'BaseApplication', then they all hide the virtual 'saveToDb'.
Is this possible? (also - are there any pitfalls I should be aware
of? -

I am sure the pitfalls are plenty. Possible? Yes. Is that what you
want? Probably not. Perhaps you want to explain what it is you're
trying to accomplish...

V
 
B

Bit byte

Victor said:
So, you essentially have

class BaseApplication
{
public:
virtual void saveToDb(const char*);
};

, right? This is much easier to understand.
<snip>

I have this :

class BaseApplication
{
protected:
virtual void saveToDb(const char*);
};

class A: public BaseApplication {

public:
void saveToDb( const struct_for_a*) ;
};

//idea behind this is that B is a type of application that will
//need to persist many different types of data (polymorphically)
class B: public BaseApplication {

public:
void saveToDb( const struct relevant_for_b*) ;
void saveToDb( const struct another_struct_for_b*) ;
void saveToDb( const struct yet_another_for_b*) ;
};


I want to know if I can do this. I believe I have done something
similar to this many moons ago, but I don't quite remember.
 
V

Victor Bazarov

Bit said:
<snip>

I have this :

class BaseApplication
{
protected:
virtual void saveToDb(const char*);
};

class A: public BaseApplication {

public:
void saveToDb( const struct_for_a*) ;

This member does not override the 'saveToDb' you declared in the base
class. It _hides_ it. If you intended to override the function, you
need to keep its signature the same, IOW, the argument has to be the
same -- const char*. As soon as you changed it, the function is not
the overrider any longer.

};

//idea behind this is that B is a type of application that will
//need to persist many different types of data (polymorphically)
class B: public BaseApplication {

public:
void saveToDb( const struct relevant_for_b*) ;
void saveToDb( const struct another_struct_for_b*) ;
void saveToDb( const struct yet_another_for_b*) ;
};


I want to know if I can do this. I believe I have done something
similar to this many moons ago, but I don't quite remember.

You can do this. It has nothing to do with polymorphism, but it is
perfectly legal.

#include <iostream>
struct A {
virtual void foo(const char*) { std::cout << "A::foo()\n"; }
};

struct B : A {
void foo(int) { std::cout << "B::foo()\n"; }
};

struct OK : A {
void foo(const char*) { std::cout << "OK::foo()\n"; }
};

int main() {
B b;
OK ok;

A *pa = &b;
pa->foo("abc"); // base class function is called

pa = &ok;
pa->foo("def"); // derived class function is called
}

V
 
B

Bit byte

Victor said:
This member does not override the 'saveToDb' you declared in the base
class. It _hides_ it. If you intended to override the function, you
need to keep its signature the same, IOW, the argument has to be the
same -- const char*. As soon as you changed it, the function is not
the overrider any longer.





You can do this. It has nothing to do with polymorphism, but it is
perfectly legal.

#include <iostream>
struct A {
virtual void foo(const char*) { std::cout << "A::foo()\n"; }
};

struct B : A {
void foo(int) { std::cout << "B::foo()\n"; }
};

struct OK : A {
void foo(const char*) { std::cout << "OK::foo()\n"; }
};

int main() {
B b;
OK ok;

A *pa = &b;
pa->foo("abc"); // base class function is called

pa = &ok;
pa->foo("def"); // derived class function is called
}

V

Thanks for the clarification Victor. I'll have to refresh my memory on
the differences between overiding and hiding - thanks for pointing out
the difference.
 
T

thomas0033

hi, Bit byte, ....just require the polymorphism of data.
did you think that why virtual, it's enough the overload between the
three functions in class b, isn't it.

we always say virtual function is used to implement polymorphism,
but 'real' polymorphism is used to implement the dynamic link library,
just my opinion.
because of virtual, we can add some new feature into our old problem
without replacing of main program(.exe).
l think more detail, it will includes something about vtbl (virtual
table), virtual methods' memory layout.
and about this, I have no more understanding.

a book:<COM+ Programming: A Practical Guide Using Visual C++ and ATL>
is good.
the first chapter <The Component Model>, you can learn some about
virtual methods.

and: <Inside the C++ Object Model>,it's worth reading!!!

it's possible because of some difference between culture,maybe my words
is ambiguous.
but that two books is Worth to read ! definitely Good Book!

thomas lee
panda's hometown, China
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top