Zaki Mirza’s Blog


… About software and beyond!

“protected” usage

I know the title is weird, but i couldnt think of anything better. So here i was coding one of my CG assignments. It was a simple assignment though im very intrigued by game/software graphics engines so i plunged in to make my own. Its in very basic stages for now. Right now im going to talk about a problem i encountered.

Consider this, you have a class called ‘BaseObject’ which is basically for a graphical object. Now graphical objects are represented by points/vertices and how surfaces/lines are drawn between those vertices. (in my case those were indices of vertices in order. ill tell you what i mean in a sec). Now how i see it the client of the class shouldnt be allowed to create an 3d/2d object without specifing the vertices,indices and the kind of surface/line between the vertices(at the least, because otherwise it doesnt make sense for a graphical object to exists without a single vertex). The Draw() method would take care of drawing. (im using opengl as my graphics api and sdl for windowing and message handling).  So in a very usual case you’d refrain from making a default constructor of the BaseObject class and at the least it would take the array of vertices,indices and the draw method (e.g. GL_POLYGON, GL_LINES etc). So far, the code would be like this:

class BaseObject
GLfloat* _vertices;
short _numVertices;
GLint* _indices;
short _numIndices;
GLint _drawMode;

int numVertices,int numIndices,GLfloat vertices[],GLubyte indices[],GLfloat colors[],GLenum drawMode);virtual void Draw();
 Now consider this. I want to make an specialization of this class, say a SolidCube class. So instinctivly i would inherit our SolidCube class from BaseObject class, let use tell me the center of the cube, the width,height and depth and pass that to base class which will handle the drawing. So how about:

class SolidCube : public BaseObject
SolidCube(GLfloat width,GLfloat height,GLfloat depth,Point3D cp
Now here comes the problem. When you inherit from a class you have to initialize that in the intializer list in the constructor. Given the center point, the height, the depth and width, i know how to calculate the 8 vertices of the cube. But i have no space to do the coding between the constructor definition and the initializer of base class in initializer list. e.g.

SolidCube::SolidCube(GLfloat width,GLfloat height,GLfloat depth,Point3D cp)
: BaseObject(){}
Now we intentionally excluded that default constructor from the BaseObject definition so that noone can make an empty object. (well ofcourse they can using NULL NULL for all the arrays and all, but hey we can sort that out in some other post. this is a lame design i know). So what do we do? Well my little solution was to make use of the “protected” access modifier. It lets you secure the variable/function for the derived classes. In this case, making BaseObject’s defualt constructor protected, i make sure that the clients of the class cant use this constructor but all the derived classes can. So now the above code can work and the derived class can manually set the vertices array after working out the vertices. By the way comming back to why im using indices ill write another post on that just right now.

The visual studio will let you do something like:

BaseObject a();

and if you try to do a.Draw() or anything with “a” it will give you a compile time error. It will give you a warning though when you try to do the above thing.

Filed under: C++, Code, programming

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

RSS Google Shared Items

  • An error has occurred; the feed is probably down. Try again later.

RSS Google Reader Starred Items

  • An error has occurred; the feed is probably down. Try again later.

Top Clicks

  • None
%d bloggers like this: