473,326 Members | 2,127 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,326 software developers and data experts.

need of virtual keyword

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
8 2873
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
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

Apr 20 '06 #4
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
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
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
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
>
""" 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
8
by: JPRoot | last post by:
Hi M. Jeffrey Tan, Just hopping you didn't forget me? :) Thanks JPRoot ----- \"Jeffrey Tan\" wrote: -----
11
by: z_learning_tester | last post by:
Hello, yes another beginner question that I'm sure is obvious to many here :-) My book is so bad. Really. It uses the exact same example of code for using the new kw and for using virtual(in the...
14
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
4
by: wizwx | last post by:
The followings are a few lines of code from the Template method in "Thinking in C++" vol.2 pp.639 class ApplicationFramework { protected: virtual void customize1() = 0; virtual void...
1
by: muler | last post by:
"If an instance method declaration includes the sealed modifier, it must also include the override modifier." The C# Programming Language, § 10.5.5 Sealed Methods Why is this? Thanks,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
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: 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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.