Re: the virtual keyword
From: Alex (mycroft_5_at_hotmail.com)
Date: 03/05/05
- Next message: Ioannis Vranos: "Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)"
- Previous message: Adrien Plisson: "Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)"
- In reply to: Michael P. O'Connor: "the virtual keyword"
- Next in thread: Matthias Kaeppler: "Re: the virtual keyword"
- Reply: Matthias Kaeppler: "Re: the virtual keyword"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 05 Mar 2005 22:56:11 +0100
On Sat, 05 Mar 2005 01:05:50 -0500, Michael P. O'Connor wrote:
> it was my understanding that if you were to declare a function as virtual
> you could not declare a body for it in the class it was declared virtual,
Then your understanding is wrong here. You can implement any method in a
class, unless it is defined as *pure virtual*. If you define a pure
virtual method, you get an abstract class, that will result in a compiler
error, when instanciated with the new keyword.
An abstract base class would be defined like this:
class a
{
public:
virtual void fun() = 0;
};
This class can only be used as a base class, calling
a* obj = new a;
would result in a compiler error. Any derived class *must* implement
a::fun()!
I think is that what you meant.
> my question is what is the benefits to this, what are the down falls to
> this? I have a use for this ability but something tells me there is
> something that I am overlooking and I might run into problems down the
> road. So am I being paranoid?
The benefit is of course, that you can inherit functions of base classes
through serveral levels of inheritance. If you define a class:
class c : public class b {
public:
void fun(void) { std::cout << "class c" << endl; };
};
this would work ok:
int main()
{
a aa;
c cc;
a *p;
p = &aa;
p->fun();
p = &cc;
p->fun();
return 0;
}
If you want to create more complex class-libraries, this would become very
handy. This only works if b::fun() is also virtual, otherwise the
inheritance chain would be broken, and the mechanism fail (c inherits
fun() from b, not from a!).
Internally the compiler represents a virtual function as a
function pointer that is added to the actual class. Even if the variable
an object is assigned to is of a base class, the pointer will be set to
the derived function, so you can call the derived function instead of that
of the base class.
The price for this is that each virtual function needs an additional
pointer size of memory. Plus the pointer has to be initiallized in the
beginning.
Usually the compiler handles all this for you, so you don't have to mess
with it. But you should keep this fact in mind, when you stream an object
at runtime, e.g. for sending it through a pipe.
BTW, as other posters mentioned before, your inline declararion would not
be compiled as actual inline code, because of the internal pointer
representation.
regards,
Alex
- Next message: Ioannis Vranos: "Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)"
- Previous message: Adrien Plisson: "Re: [OT] Re: Teaching new tricks to an old dog (C++ -->Ada)"
- In reply to: Michael P. O'Connor: "the virtual keyword"
- Next in thread: Matthias Kaeppler: "Re: the virtual keyword"
- Reply: Matthias Kaeppler: "Re: the virtual keyword"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|