By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

need of virtual keyword

P: n/a
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

Apr 20 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
rakoo wrote:
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.

Apr 20 '06 #2

P: n/a
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

Apr 20 '06 #3

P: n/a

Apr 20 '06 #4

P: n/a
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

Apr 20 '06 #5

P: n/a
rakoo wrote:
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.

Apr 20 '06 #6

P: n/a
rakoo wrote:
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
Apr 20 '06 #7

P: n/a
rakoo wrote:
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

Apr 20 '06 #8

P: n/a
>
""" 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)

Apr 20 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.