473,395 Members | 1,526 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,395 software developers and data experts.

Runtime polymorphism

It is not quite clear why the compiler uses rntime polymorphism when
the virtual keyword is specified.
Since the virtual Keyword is specified,why can't the
compiler decide at the runtime itself,which function is to be
called,even if the function is called by the base class pointer??

Apr 1 '06 #1
4 4927
arunvnk wrote:
It is not quite clear why the compiler uses rntime polymorphism when
the virtual keyword is specified.
Since the virtual Keyword is specified,why can't the
compiler decide at the runtime itself,which function is to be
called,even if the function is called by the base class pointer??


In a fully dynamic language like Ruby, every method is virtual. This implies
every method costs extra to call (more time and more memory).

C++ competes with assembler, so you don't pay for what you don't use. If C++
made every method virtual, then many projects could not use C++. Either
their programs would take too long to run, or a C++ compiler would take too
long to compile everything and analyze what methods should be virtual and
which could bind early.

So, you just omit 'virtual' on every method that you know won't need it.
This is no burden, because 'virtual' in C++ acts as a comment meaning
"override me if you want to re-use this class with different behavior."

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 1 '06 #2
ar*****@gmail.com wrote:
It is not quite clear why the compiler uses rntime polymorphism when
the virtual keyword is specified.
Since the virtual Keyword is specified,why can't the
compiler decide at the runtime itself,which function is to be
called,even if the function is called by the base class pointer??

ar*****@gmail.com wrote: It is not quite clear why the compiler uses rntime polymorphism when
the virtual keyword is specified.
Since the virtual Keyword is specified,why can't the
compiler decide at the runtime itself,which function is to be
called,even if the function is called by the base class pointer??


hi,
actually (in polymorphism) you can use the base class pointer to invoke
the functions of
derived class object during run time...

NOW , which object's function to invoke is determined at run time,
(there is a vtable
mechanism involved) compiler makes no assumptions about it during
compilation since the
base class pointer might point to some other object at some other time
and invoke that
object's function.

this whole process takes place during run time.

consider this:

class BASE{
.........};
class derived1:public BASE{.............};
class derived2:public BASE{.............};

BASE *bptr= new derived1;//object is created during run time
bptr->anyfun(); //anyfun of derived1 is called
//now bptr can also point to an object of derived2 ,and future
resolutions
// are determined at run time

Apr 1 '06 #3

<ar*****@gmail.com> wrote in message
news:11********************@i40g2000cwc.googlegrou ps.com...
| It is not quite clear why the compiler uses rntime polymorphism when
| the virtual keyword is specified.
| Since the virtual Keyword is specified,why can't the
| compiler decide at the runtime itself,which function is to be
| called,even if the function is called by the base class pointer??
|

Pointers don't call functions, its the object at the pointer that is
doing the calling. A base pointer simply defines that *if* it is
pointing to an object, that object can be either a specific type or a
derivation thereof. The compiler enforces the type checks, nothing more.
ie:
If the ptr is a shape*, then the triangle satisfies the condition (a
triangle "is_a" shape). The triangle knows how to calculate its own area
and volume. The pointer and the compiler haven't got a clue how to do
that.

A pointer doesn't even know if its actually pointing to anything at all.
Thats why using pointers is dangerous and needs to be strictly confined.
This is not so with a reference. A reference knows its object and its
type. But thats another topic.

Incidentally, the virtual keyword essentially says: generate a vtable
for each of these related family of objects. Vtables are usually
themselves dumb pointers too.

At runtime, the compiler needs not know whether a particular object is
this or that derivation since its the eventual object, and therefore the
inheritance branch, that handles the calls and invocations. The compiler
simply governs and polices the type-transcations the safest way it can.

Thats a powerful concept since you are now free to create a system based
on abstract or base element-types that can be expanded to include new
derivatives that you'll bring into the design a decade from now. Without
modifying a single line of the original code except for the new
features. Thats because the compiler is decoupled from the underlying
system.

Apr 1 '06 #4
ar*****@gmail.com posted:

Since the virtual Keyword is specified,why can't the
compiler decide at the runtime itself,which function is to be
called,even if the function is called by the base class pointer??


That's exactly the aim: If you declare the function as "virtual", then the
compiler will make sure that the correct function is invoked, regardless of
the type of the expression.

By having the "virtual" keyword in C++, we are simply given the choice as to
whether we desire this behaviour.

Choice is good.

For instance, if we are writing a carwash program, then we can decide that
we'll wash a certain vehicle the way in which a car should be washed... or
we can just give it the generic vehicle wash:

void PerformCarwash( Vehicle& vehicle )
{
int money = AcceptMoney();

if ( 5 <= money ) WashVehicle( vehicle.GetWashingInfo() );
}

If the member function "GetWashingInfo" is virtual, then, if this vehicle is
a car, then it will be washed in a manner fitting a car. If however
"GetWashingInfo" is non-virtual, then it will just be given the generic
vehicle wash.
-Tomás
-Tomás
Apr 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
4
by: AdamM | last post by:
How can I change an object's type at runtime? For example, here's what I want to do in psedocode: object animal; if (dog) { animal=(dog)animal;
4
by: LP | last post by:
Hi, I understand the concept/definition of polymorphism. But what does the term "runtime polymorphism" mean? I was asked to define it during a technical interview. I gave a guy vanilla definition...
2
by: eric.dennison | last post by:
In the sample below: testClass is derived from object. We can cast object to testClass, no problem We can cast testClass to object no problem Compiler is ok with cast object to testClass but...
7
by: doina | last post by:
Hello, Can I have runtime polymorphism using references. I knew that runtime polymorphism could be obtained only by pointers, but now I have tried this in Visual C++ 8.0: #include <iostream>...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
16
by: desktop | last post by:
I have read that using templates makes types know at compile time and using inheritance the types are first decided at runtime. The use of pointers and casts also indicates that the types will...
8
by: Ralf Goertz | last post by:
Hi, I want to do able to declare an object bar of a templated class foo where the actual template type of foo is only know at runtime: ----- #include <string> template <class Tclass foo {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.