|
From: Somanathan K. <som...@wi...> - 2003-07-21 11:26:49
|
I hope you are familiar with pointers. Its needs a good understanding of
pointers. See the below example,
Class base=20
{
public:
virtual void f1(){=20
...body ...
}
};
class derived1 : public base
{
public:
virtual void f1(){ // virtual or not doesn't matter here
...body...
}
}
class derived2 : public base
{
public:
virtual void f1(){
... body...
}
}
int main()
{
base* b;
derived1* d1 =3D new derived1;
derived2* d2 =3D new derived2;=20
b =3D d1;
b->f1(); //this calls the derived1 f1
b=3Dd2;
b->f1(); //this call the derived2 f1
// u should delete the objects....
} =20
this is a simple example, but in actual usage, it will be bit different
(Objects may be pooled in a list/array etc.). =20
-----Original Message-----
From: pr...@le... [mailto:pr...@le...]=20
Sent: Monday, July 21, 2003 3:34 PM
To: dev...@li...
Subject: [Dev-C++] Virtual functions?
Hi List,
I've been through numerous books & websites on this topic, but I _still_
can't quite understand what is the purpose of a virtual function.
From what I understand, a virtual function is defined in a base class,
but
isn't implemented there. The implementation goes in the derived class.
Now
if this is true, then why should I need to define a function in my base
class at all? Since I'm eventually going to implement it elsewhere, I
might
as well define it there too.
Am I even on the right track here? Can someone take me back to base
again?
Thanks.
Regards,
Pranav Negandhi
-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|