|
-
February 12th, 2003, 02:00 AM
#1
Abstract Class problem
CMy is a abstract class. As per abstract class definition we can not make any instance of that. But it is not giving any error in MSDEV IDE.
#include <iostream.h>
class CMy
{
public:
CMy(){}
CMy(int i)
{
this->i=i;
};
~CMy()
{
};
virtual void show() const =0;
int i;
};
void show(CMy &myObject)
{
myObject.i = 7;
}
void main(int argc, char* argv[])
{
CMy(9);
show(CMy(6));
}
-
February 12th, 2003, 02:37 AM
#2
well sionce the class has a constructor and a destructor it's not abstract.
It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames
-
February 12th, 2003, 04:01 AM
#3
well sionce the class has a constructor and a destructor it's not abstract.
But there is a pure virtual function. AFAIK it's meaning that CMy is a abstract class.
-
February 12th, 2003, 04:07 AM
#4
Originally posted by deesan
But there is a pure virtual function. AFAIK it's meaning that CMy is a abstract class.
Okay. You CAN call it abstract but the constructor and destuctor allow you to create it.
It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames
-
February 12th, 2003, 05:11 AM
#5
We don't need to declare constructors and destructors for all classes.
If some class don't have one of them, then compiler will generate default constructor (destructor)
So if class have not constructor, it's not meaning that we can't create variable of this class.
Am I wrong???
-
February 12th, 2003, 05:23 AM
#6
No, no, no, no, no.
You cannot create an object of abstract class type no matter how many constructors you provide.
Code:
// Two things: don't use the ".h" forms of these includes
// and why is it here anyway?
#include <iostream.h>
class CMy
{
public:
CMy(){}
CMy(int i)
{
this->i=i; // Use an initialiser list
}; // this semicolon is redundant.
~CMy() // If you're going to make this class abstract,
// you really ought to make this virtual
{
}; // this semicolon is redundant.
virtual void show() const =0;
int i; // You really should make data members private
};
void show(CMy &myObject)
{
myObject.i = 7;
}
void main(int argc, char* argv[])
{
CMy(9);
show(CMy(6));
}
It's possible that the compiler optimises away both temporary objects, but it really ought to generate an error. There must be more to this. It's probably something to do with temporaries and references - anyone with a copy of the standard able to explain it? Unless it's a bug in VC++.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
February 12th, 2003, 05:26 AM
#7
Note that if you declare an actual object in main() - e.g. "CMy f(6)", then compilation fails with expected errors about abstract classes.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
February 12th, 2003, 11:58 PM
#8
Hi folks,
I am asking only one thing.
Is it MSDEV ID(VC++) bug or i could be happened. that was the
sample program so there could be some flaw but here my concern abt abstract class behavior .
Thanks for yours input.
Saurabh
-
February 13th, 2003, 12:49 AM
#9
VC6 seemed to be an issue so I searched and found
a good discussion here to determine if it is a bug. Slightly different
from this in syntax( several different methods )but still generating
a ctor call
http://www.experts-exchange.com/Prog..._20250027.html
With VC7 that the correct compiler error is now generated
for the code you posted here.
Last edited by mdmd; February 13th, 2003 at 12:57 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|