Hello all,
I am writing a base class that will be inherited by other classes. However, I do not want this class to be instantiated directly. I only want it to be inherited from. Is this possible?
Thanks, dene.
Printable View
Hello all,
I am writing a base class that will be inherited by other classes. However, I do not want this class to be instantiated directly. I only want it to be inherited from. Is this possible?
Thanks, dene.
Yes there is use something like the following code segment.
Code:virtual class MyClass
{
public:
int get_data();
virtual void my_function();
private:
int data;
};
http://msdn.microsoft.com/library/de...m/deriv_15.asp
Although you could still have pointers or references to a class object
"Although you could still have pointers or references to a class object". Does this mean that I can still do: MyClass *t = new MyClass()?
No...First of all it would be:
MyClass* T = new MyClass
and second, you could not do it anyway :)
Ok, thanks for everyones help.
virtual class ..
???
I doubt that's allowed in C++. Abstract classes have at least one pure virtual method:
virtual void pure() = 0;
>I doubt that's allowed in C++.
It isn't. But it could just be a new feature recently added to the language by the extensions committee. You know how much they love adding new features. ;)
The pointers you can use like this:
And if you want a class that should be an abstract data type, but none of the member functions lend themselves to being pure virtual, you can have a pure virtual destructor.Code:class A
{
virtual void foo( ) = 0;
};
class B : public A
{
void foo( ) { }
};
// Somewhere else...
A* obj = new B; // ... = new B( ); would also work, though
You could make the constructor protected.
Nope, I don't think it's a new feature its a typo sometimes I break into an accidental Java type code except in Java the class is preceeded with the keyword abstract. Thanks for catching that CornedBee!
True... But it is still possible to instantiate it (static builder function, for example). Granted, the particular application will determine if this makes sense or not in the context of the program.Quote:
Originally Posted by Sang-drax