need of virtual keyword

R

rakoo

I want to question about this virtual keyword , what is neccessty of it
..
when base class ponter or simply object assingned to derived class
object ,we never want
that base class funtion by base class object or pointer which pointing
to derived class object

Are we ?

then why stroupstrop has given this keyword

can't we handle this situation

if base class pointing to derived class automatically use run time
binding or make every funtion virtual automaticaly if it involves
inheritance .


Rakesh Kushwaha
 
R

Rolf Magnus

rakoo said:
I want to question about this virtual keyword , what is neccessty of it
.
when base class ponter or simply object assingned to derived class
object ,we never want
that base class funtion by base class object or pointer which pointing
to derived class object

Are we ?

then why stroupstrop has given this keyword

virtual functions impose some overhead, and in C++, you don't pay for things
you don't need. So when a function doesn't need to be polymorphic, you just
don't make it virtual.
 
R

rakoo

why programmer has worry about putting virtual keyword .

when it's already known that whatever base class pointer is pointing
it should call funtion according to that .

Rakesh Kushwaha
 
R

rakoo

why overhead belongs to putting virtual keyword .

why programmer has worry about putting virtual keyword .

when it's already known that whatever base class pointer is pointing
it should call funtion according to that .


Rakesh Kushwaha
 
C

Carlos Martinez

rakoo said:
why overhead belongs to putting virtual keyword .

Because address of virtual function must be established at execution
time instead of compile time of non-virtual ones.
why programmer has worry about putting virtual keyword .

I think it is an inconvenient, because many times developer doesn't know
if in the future will be necessary, and making all functions in base
class virtual maybe a good choice.
But think that in C++, Base classes doesn't act only as interfaces (may
have code) and its particular implementation in Base, doesn't need to be
virtual in many cases, then you can't imposse that overhead in that cases.
when it's already known that whatever base class pointer is pointing
it should call funtion according to that .

Not in every case. That's typical in public interface but in private
interface is not so typical.
 
T

Tom Widmer

rakoo said:
I want to question about this virtual keyword , what is neccessty of it
.
when base class ponter or simply object assingned to derived class
object ,we never want
that base class funtion by base class object or pointer which pointing
to derived class object

Are we ?

then why stroupstrop has given this keyword

can't we handle this situation

if base class pointing to derived class automatically use run time
binding or make every funtion virtual automaticaly if it involves
inheritance .

All you are suggesting is that every member function should be
implicitly virtual. However, this is very suboptimal, since unless the
compiler employs extensive whole program optimization, most calls
through pointers or references would have to be dynamically dispatched.

Whole program optimization is expensive (perhaps prohibitively so on
large, 1 million+ LOC projects), and prevents the use of separate
compilation (libraries, dlls, etc.).

Tom
 
M

Michiel.Salters

rakoo said:
I want to question about this virtual keyword , what is neccessty of it
.
when base class ponter or simply object assingned to derived class
object ,we never want
that base class funtion by base class object or pointer which pointing
to derived class object

Are we ?
In fact, we want to. We want it so much, that even if a function IS
virtual,
it's possible to call it non-virtually.

C++ is by design a language that gives you lots of choices. It's up to
you
to make good choices. Virtual is one choice you have to make, and if it
was the default we'd want a keyword "nonvirtual".

HTH,
Michiel Salters
 
A

al pacino

""" when it's already known that whatever base class pointer is
pointing
it should call funtion according to that . """

noo its not like that,
what happens is even if base pointer is currently pointing to
a derived object the function invoked wud be that of base class and not
of
derived class.

thats why u need virtual function mechanism(polymorphism)
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top