Advertisement

Purely Virtual Class

Started by April 14, 2002 06:02 PM
4 comments, last by haro 22 years, 10 months ago
In VC++ is there a way to define a purely virtual class? Say I want a class called Object (reserved keyword in C++ anybody?), and I want to use it to reference 3d objects, but I want it totally virtual... ie- class Object { public: virtual void toggleTexture; virtual void render(); // Obtain balance. virtual void toggleShow(); // Default implementation. }; Is there a way to define the class as purely virtual? Ie- Virtual class Object Why does it really matter if a class is completely virtual as opposed to just using basic inheritance? I''m anal. A small code snippet would be useful, or just advice here. Thanks alot. p.s. I realize this it not a C++ forum, but this is an OpenGL related question, and there seems to be more informed people on this board than most...

And yes I have searched through docs already. They do not mention constructing completely virtual classes, but I would assume it is available.
Advertisement
A pure virtual class in C++ :


class a_class
{
virtual void something(int a) = 0;
}

The class has to have at least one function with = 0 behind it.
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
Yes, as someone else stated, you can have a purely virtual class in C++ by having a "= 0;" after the function declaration.

class foo
{
virtual void bar(void) = 0;
};

You then don''t provide a body for the function, leaving that to child classes.

The POINT of having a virutal class is that you cannot actually create an instance of a virtual class, only it''s concrete child classes (those that have created bodies for all the functions declared with "= 0".

So you can''t create a foo:

foo a; // compiler error

but if you had a child class...

class baz : public foo
{
virtual void bar(void);
};
void baz::bar(void) {...}

then you can create a baz:

baz b; // legal

Got all that?


You can still declare pointers and references to foo... which is the reason you''d create a virtual class in the first place. And you''d make a class virtual (as opposed to a regular base class) when there''s no reasonable default behavior.

Lets say you wanted a group of classes to be read to and written from disk... you could do something different in each class, or you could have them all inherit from a virtual base class:

class IOCapable
{
public:
virtual void Write() = 0;
virtual void Read() = 0;
};

So you could keep a list of IOCapable pointers to all the classes you wanted to send to and from disk... then just loop through that list calling Read or Write...

something along the lines of:

void WriteAll(IOCapable *writeUs, int numObjs)
{
for(int i = 0; i < numObjs; ++i)
{
writeUs->Write();
}
}

Get the idea?

--Mark Storer
Software Engineer
Cardiff Software

#include <disclaimer>
typedef std::disclaimer<Cardiff> Discard;
I think you guys are missing something.

C++ does not have virtual classes, no mainstream language does by default but there are some projects that have incorporated it into java.

C++ has virtual functions, and that is what most people here have shown.

A virtual class is an extremely powerful concept. Imagine writing an engine as a class.. we will call it Engine. From engine several classes extend that have different functionality.

Now lets say you licensed this engine and you wanted to rewrite some parts. Lets call this new engine Blah. To create this engine you simply do a class Blah extends Engine, and inherit the whole engine. Then say the original engine had a skeletal animation system that was done in software and you wanted to rewrite it for vertex shaders, optimize, re-design, etc. Just a simple class BoneMesh extends Blah; will do it. The new engine will use this class now.

I don''t have time to go into deep detail about all of this, but Sweeney has gone into detail about this already in an old Gamespy article. It was in year 2000 I believe, check there.

Do some reading on this type of stuff, it is very interesting. Shouldn''t be too long before we are using languages with real parametric polymorphism (no templates are not all there is to this concept =D), virtual classes, etc. etc. I am trying to implement this type of stuff into my scripting language, but I haven''t had nearly enough time to give it a real go.
"All programmers are playwrights and all computers are lousy actors." -Anon.
Hi,

additionnaly, you could put a virtual destructor :

class Object{public:virtual void toggleTexture() = 0;virtual void render() = 0; // Obtain balance.virtual void toggleShow() = 0; // Default implementation.virtual ~Object() {}}; 


----
David Sporn AKA Sporniket

This topic is closed to new replies.

Advertisement