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

virtual mechanism acivation

virtual mechanism is activated on pointers only , not on objects .
what is reason beind this .

Regards
Mangesh .

May 25 '06 #1
4 1484
mangesh posted:
virtual mechanism is activated on pointers only , not on objects .
what is reason beind this .

Regards
Mangesh .

Explain yourself in more than the bare minimum amount of words and maybe
people will be more enthusiastic to help you.

Virtual functions retain their "virtualism" regardless of how they're
called. (unless ofcourse you explicitly specify that you don't want
virtualism, e.g.:

Derived derived;

Base &base = derived;

base.Base::Func();
-Tomás
May 25 '06 #2
mangesh wrote:
virtual mechanism is activated on pointers only , not on objects .
what is reason beind this .


It is also "activated" on references, just as well.

The reason is simple. If you have an object, there is no need to
"activate" any "virtual mechanism" because the type is known at the
compile-time. With pointers the "real" type, the type with which
the top-most object was created, is unknown at compilation. Hence
the need (or the capability) to dispatch those calls dynamically.

What I mean is that when you have a pointer (or a reference) to some
class, the compiler thinks that it can be working with a sub-object
of another object, with an object of a base class, whereas the "real"
object is of the derived class. There is a way to know, mind you,
but it involves the same "virtual mechanism", and it's only possible
to know at run-time, not compile time.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 25 '06 #3
Tomás posted:
mangesh posted:
virtual mechanism is activated on pointers only , not on objects .
what is reason beind this .

Regards
Mangesh .

Explain yourself in more than the bare minimum amount of words and

maybe people will be more enthusiastic to help you.

Virtual functions retain their "virtualism" regardless of how they're
called. (unless ofcourse you explicitly specify that you don't want
virtualism, e.g.:

Derived derived;

Base &base = derived;

base.Base::Func();

After reading Victor's response, I realised that you were most likely
referring to the compiler actually churning the gears to dereference a
function pointer and so forth... so, to clarify:

Derived derived;

derived.SomeVirtualFunc(); /* No need to consult V-Table */
Derived * const p = &derived;

p->SomeVirtualFunc(); /* Gears start churning (however the compiler
can cleary see where the pointer got
its value from, so it'll probably optimize away
the necessity to consult the V-Table */
Derived &ref = derived;
ref.SomeVirtualFunc(); /* Gears start churning (chance for
optimization again here */
-Tomás
May 25 '06 #4
mangesh wrote:
virtual mechanism is activated on pointers only , not on objects .
what is reason beind this .


Think about it. Try to find an example where

1) you have an automatic object
2) you call a member function on it
3) that member function would be different if the "virtual mechanism"
was "activated"

Compare a case where you call a member function through 1) a pointer or
a reference and 2) an object. Do you see a difference? Do you think it
is possible for a pointer to a Base (Base*) to point toward something
else than a Base? Could a Base* actually point toward a Derived object?
(hint: yes)

Now, do you think it is possible for an automatic object (that is, an
object on the stack) to "point to" something else? Is an automatic
object's static type (at compile time) different from it's dynamic type
(at runtime)?

When you have an automatic object, its type is known at compile time.
The member functions called on it are thus bound at compile time also.
The static type of a pointer (or reference) may be different from its
dynamic type. That's what virtual functions are for: they are called on
the dynamic type of the object. Functions are therefore "decided" (or
"dispatched") at runtime.
Jonathan

May 25 '06 #5

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

Similar topics

8
by: Gerhard Wolfstieg | last post by:
The following situation: class A0 { static A0 *a0; // something like this to publish the pointer public: A0() { a0 = this; } virtual ~A0(){}
3
by: Sensorflo | last post by:
struct CBase { virtual inline void foo() { puts("base"); } }; void main() { CBase obj; (&obj)->foo(); // static call or } // dynamic (using virtual table) call ? Is it...
4
by: DaKoadMunky | last post by:
I was recently looking at some assembly code generated by my compiler. When examining the assembly code associated with constructors I noticed that the dynamic-dispatch mechanism was enabled...
3
by: Steven T. Hatton | last post by:
I stumbled upon this blog while googling for something. I have to say, I really don't understand what Lippman is trying to tell me here. I included the first paragraph for context, but the second...
5
by: Devika | last post by:
hi, I understand the need of virtual destructor.but virtual constructor is not supported in c++.but basically why we need to have virtual constructor?? thx in advance Devika
2
by: Neelesh | last post by:
I wanted to know why member templates cannot be virtual - is it because there is a conceptual impossibility in having them, or is it because the current mechanism of implementaing virtual functons...
2
by: Ashwin | last post by:
hi guys, can anyone explain this class Base { public: virtual void foo() = 0; virtual void bar() = 0; };
23
by: Dave Rahardja | last post by:
Since C++ is missing the "interface" concept present in Java, I've been using the following pattern to simulate its behavior: class Interface0 { public: virtual void fn0() = 0; };
7
by: Christopher Pisz | last post by:
My problem is my derived class is getting called twice instead of the base and then the derived. I thought this was the purpose for virtuals and dynamic casting :/ I want my base class to have its...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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.