 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
vasanth kumar Guest
|
Posted: Wed Sep 29, 2004 8:48 am Post subject: pure virtual |
|
|
I am writing a small example below. I want to know, is this allowed as per
the standard C++. We make an abstract class by making any membor method as
pure virtual. when we make it pure virtual, what is the meaning of providing
definition as shown below. are the code lines in bold are good practice.
class Vehicle {
public:
virtual void startEngine() = 0;
virtual ~Vehicle() {};
};
void Vehicle::startEngine()
{
}
class Car : public Vehicle {
public:
void startEngine(){
Vehicle::startEngine();
}
};
void main()
{
Car objCar=new Car();
objCar.Vehicle::startEngine();
}
Thanks,
Vasanth
|
|
| Back to top |
|
 |
Sharad Kala Guest
|
Posted: Wed Sep 29, 2004 9:06 am Post subject: Re: pure virtual |
|
|
"vasanth kumar" <vasanth.kumar (AT) eds (DOT) com> wrote in message
| Quote: | I am writing a small example below. I want to know, is this allowed as per
the standard C++. We make an abstract class by making any membor method as
pure virtual. when we make it pure virtual, what is the meaning of
providing
definition as shown below. are the code lines in bold are good practice.
|
Providing definition for pure virtual functions is legal. It's not a
question of good/bad practice but more to do with your requirements.
Consider this code -
struct base
{
// ...
virtual ~base() = 0; //Definition required
};
struct derived : base
{
};
int main()
{
derived d;
}; // Linker error
Always, int main ()
Sharad
|
|
| Back to top |
|
 |
vasanth kumar Guest
|
Posted: Wed Sep 29, 2004 9:29 am Post subject: Re: pure virtual |
|
|
thanks for the reply.
yes, destructor case I understand. provides default destructor. can anyone
think of a requirement for providing this definition for other member
functions.
-Vasanth
-------------
"Sharad Kala" <no_spam.sharadk_ind (AT) yahoo (DOT) com> wrote
| Quote: |
"vasanth kumar" <vasanth.kumar (AT) eds (DOT) com> wrote in message
I am writing a small example below. I want to know, is this allowed as
per
the standard C++. We make an abstract class by making any membor method
as
pure virtual. when we make it pure virtual, what is the meaning of
providing
definition as shown below. are the code lines in bold are good practice.
Providing definition for pure virtual functions is legal. It's not a
question of good/bad practice but more to do with your requirements.
Consider this code -
struct base
{
// ...
virtual ~base() = 0; //Definition required
};
struct derived : base
{
};
int main()
{
derived d;
}; // Linker error
void main()
Always, int main ()
Sharad
|
|
|
| Back to top |
|
 |
Sharad Kala Guest
|
Posted: Wed Sep 29, 2004 9:34 am Post subject: Re: pure virtual |
|
|
"vasanth kumar" <vasanth.kumar (AT) eds (DOT) com> wrote in message
| Quote: | thanks for the reply.
|
You are welcome, please don't top-post.
| Quote: | yes, destructor case I understand. provides default destructor. can anyone
think of a requirement for providing this definition for other member
functions.
|
Sure, Herb Sutter gives 3 cases in which pure virtual functions could have a
body - http://www.gotw.ca/gotw/031.htm. He also says that the destructor
example is the most common.
Sharad
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|