Advertisement

Virtual?

Started by January 19, 2003 05:42 PM
56 comments, last by John Dowis 21 years, 9 months ago
quote: Original post by John Dowis
One part I don''t understand is:

virtual void _stdcall fx(void) = 0;

The main problems I am having with this are the parts "virtual"

virtual is a C++ keyword to indicate which functions will be despatched dynamically. If a function is virtual, it means that the actual function which gets called is determined at runtime, according to the dynamic type of the object receiving the function call. For example, if you have this simple hierarchy:

  class B{public:  virtual void foo() { /* ... */ }};class D : public B{public:  void foo() { /* ... */ }};  

Public inheritance of B indicates that you can use a D wherever a B is expected, since D implements a superset of B''s behaviour.

Then, say you have this code:
B *b1 = new B;B *b2 = new D;  // a B is expected, but create a Db1->foo();b2->foo(); 

Since foo is marked as virtual, it will get despatched based on the dynamic type of the object. The dynamic type is what appeared on the right-hand side of the new expression. So, for b1->foo(), B::foo gets called and, for b2->foo(), D::foo gets called. This is a limited form of polymorphism, known as "subtype polymorphism".
quote:
"_stdcall"

When a function gets called, there are many ways of passing parameters. Pretend you have this function signature:
int foo(int x, int y); 

Where do the x and y get their values from? How does the callsite know how to retrieve the return value? You might push y onto the stack, followed by x, and then have foo pop them back off. Or you might push x followed by y. You might put them into registers, or indirect via a memory location. There are many permutations of how you might do this, and "stdcall" denotes one way of doing it. Check out your compiler''s documentation to find out more.
quote:
I am also wondering why he put "void" as a parameter to the function "fx"

Because he''s into C habits. It''s not necessary in C++.
quote:
why (or how he even can) set it equal to anything, in this case 0.

That confusing syntax is not an assignment. If a function declaration in a class is followed by "=0", it means the function is "pure virtual". Pure virtual means "any classes which inherit me *must* implement this function if they are to be instantiated". If an implementation of the function is not provided, then an attempt to create an instance will fail, i.e. the class is "abstract".

BTW, this is all basic C++, not C. I suggest that you ask whoever "entirely trained you in C++" for your money back.
quote: Original post by Kamikaze15
[...] I''m not very familiar with class inheritance but I hope this will help you get started...

What exactly are you trying to demonstrate? It''s not how virtual works, that''s for sure. All of your function calls are fixed-up at compile-time.
Advertisement
By using dynamic despetching is that slower then what kamikazee suggested? Since the compiler knows during compililation time on what function to use it can be optimized when compiled but the method you showed us it is determined at runtime.
quote: Original post by ph33r
By using dynamic despetching is that slower then what kamikazee suggested?

Whether something is "slower" only makes sense if the two features being compared achieve the same ends. Static despatching is obviously different to dynamic despatching, so you should not make a direct comparison. If you have to despatch a function based on the dynamic type of an object, you simply cannot use static despatch; you have to pay for dynamic despatch whichever way you look at it. Kamikaze15''s example was completely irrelevant to John Dowis''s questions.
quote:
Since the compiler knows during compililation time on what function to use it can be optimized when compiled but the method you showed us it is determined at runtime.

Yes. Usually, it will be indirected via a pointer to the vtable. However, if you don''t want dynamic despatch then you shouldn''t use it. It just so happens that "virtual" competes with inheritance for the "most commonly misused feature of C++" crown.
quote: virtual" competes with inheritance for the "most commonly misused feature of C++" crown


Hate to pester you - but can you please expand on that? Why is virtual the most misused feature of c++? I am using virtual functions in my game currently via dynamic despatching and I'm hoping I'm not using it incorrectly

error: 500

[edited by - ph33r on January 20, 2003 10:18:45 AM]
quote: Original post by ph33r
Hate to pester you - but can you please expand on that? Why is virtual the most misused feature of c++?

Because people often mark functions as virtual "just in case". Instead of being decisive, they say "well, someone might want to inherit from this class, so I''ll make the functions virtual", and then they go on to make *everything* virtual. Take a look at the C++ Standard Library for various examples of how you should implement classes. E.g. how many virtual functions does std::basic_string have?
Advertisement
Right I can understand that and to further testify to your point in a few books I've read it even said "as a general rule if you ever might plan on using a derived class that will overload those functions make them virtual" so I can see how many people mark many functions as virtual, but whats the big deal?

Like you said if the function is virtual and they don't use dynamic despatching then it will be decided during compile time what function it calls and there will be no slow down in speed. So making a function virtual "in case" has no real effect that I can tell.

[edited by - ph33r on January 20, 2003 10:50:29 AM]
Opss, sorry for my bad reply, I''m not very familiar with virtual stuff too...

KaMiKaZe
quote: Opss, sorry for my bad reply, I''m not very familiar with virtual stuff too...

Kamikaze15 don''t feal bad - John Dowis showed a lack of understanding of any type use of virtual functions so your example wasn''t that inappropriate
quote: Original post by ph33r
I can see how many people mark many functions as virtual, but whats the big deal?

There''s two (potential) problems. Firstly, if a function doesn''t need to be virtual, then it''s unnecessary overhead to make it virtual. It *will* get dynamically resolved in some circumstances, and it can bump up the size of the class. That''s sometimes an issue for the legions of premature optimisers who so love C++, and is what leads to people whining about the inefficiency of virtual functions. The second problem is that it may be indicative of sloppy thinking. Many abstract data types do not need any virtual functions, so what thinking lies behind making the functions virtual?
quote:
Kamikaze15 don''t feal bad - John Dowis showed a lack of understanding of any type use of virtual functions so your example wasn''t that inappropriate

It was inappropriate because it doesn''t show any use of virtual functions. Remove virtual from the example and it does the same thing. However, the point isn''t to make Kamikaze feel bad, or to discourage him from trying to help, just to point out to possibly confused readers that it doesn''t demonstrate what they might think.

This topic is closed to new replies.

Advertisement