virtual function doubt

H

harry

Hi,
We are using virtual keyword in base class function, when same
function name is there in base class and derived class. And we want to
call derived class same function.

my doubt is we have to use virtual function when same function and
also same parameters are there in base class and derived class.
but what we have to do if same function name but different parameters
are there in base and derived class function.
can virtual keyword help in this case also for accessing derived class
function?
should we have to use virtual here also?

thanks.
 
I

Ian Collins

harry said:
Hi,
We are using virtual keyword in base class function, when same
function name is there in base class and derived class. And we want to
call derived class same function.

my doubt is we have to use virtual function when same function and
also same parameters are there in base class and derived class.
but what we have to do if same function name but different parameters
are there in base and derived class function.
can virtual keyword help in this case also for accessing derived class
function?
should we have to use virtual here also?

Your question isn't clear, an example would help.

Are you referring to something like:

struct B
{
virtual void fn( int ) = 0;
void fn( unsigned );
};

struct D : B
{
void fn( int );
void fn( unsigned );
};
 
H

harry

Your question isn't clear, an example would help.

Are you referring to something like:

struct B
{
virtual void fn( int ) = 0;
void fn( unsigned );

};

struct D : B
{
void fn( int );
void fn( unsigned );

};

I am referring to something like:

struct B
{
void fn( int) {/* ..*/}
};

struct D : B
{
void fn( int, int ) {/*...*/}
};

same function and different parameters are there.
now should we have to use virtual keyword in base class function to
accessing derived class function? can we use virtual in this case
also? or any other method is there.

like this

struct B
{
virtual void fn( int) {/* ..*/}
};

struct D : B
{
void fn( int, int ) {/*...*/}
};
 
A

Alf P. Steinbach

* harry:
I am referring to something like:

struct B
{
void fn( int) {/* ..*/}
};

struct D : B
{
void fn( int, int ) {/*...*/}
};

same function and different parameters are there.
now should we have to use virtual keyword in base class function to
accessing derived class function?

The two functions are unrelated.

But it's unclear what you're trying to ask.

A concrete example of what it is you're trying to achieve could help.


Cheers & hth.,

- Alf
 
S

Stuart Redmann

Hi,
  We are using virtual keyword in base class function, when same
function name is there in base class and derived class. And we want to
call derived class same function.

my doubt is we have to use virtual function when same function and
also same parameters are there in base class and derived class.
but what we have to do if same function name but different parameters
are there in base and derived class function.
can virtual keyword help in this case also for accessing derived class
function?
should we have to use virtual here also?

From what I have deduced from the other postings in this thread I
assume that you want to ask the following question:
If I have the following code:
struct B
{
void fn( int) {/* ..*/}
};


struct D : B
{
void fn( int, int ) {/*...*/}
};
How can I call B::fn (int) on an object of type D?

The problem you encounter is that you cannot call the base class
function (the one that takes only one int) on an object of the derived
class. This problem is called "function hiding" and can be solved by
introducing "using" declarations:

struct B
{
void fn( int) {/* ..*/}
};


struct D : B
{
using B::fn; // <------------------------- !!!
void fn( int, int ) {/*...*/}
};

Regards,
Stuart
 
R

Ron

struct B
 {
    void fn( int) {/* ..*/}
 };

 struct D : B
 {
    void fn( int, int ) {/*...*/}
 };
How can I call B::fn (int) on an object of type D?

 The problem you encounter is that you cannot call the base class
function (the one that takes only one int) on an object of the derived
class. This problem is called "function hiding" and can be solved by
introducing "using" declarations:
Actually you can call it just the way you wrote it:
B::fn(3);

it is only the (unqualified) name that is hidden, not the function's
existence. The "using" declaration will bring the B::fn in to the
derived class to avoid hiding the name.

In these problems is best to remember how function calls are resolved.
Follow these steps:

1. Look up the name in the current scope (in the hidden case D::fn)
2. Look at overloads of that name (without the using only fn(int,int
is an overload) and select the best.
3. Then check the access control of the resolved function.
4. Then if the function is declared virtual, select the ultimate
overrider for that function.
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top