Skip to main content
gamedev.net
🔒 Locked

Restricting Functionality to Subclasses

Started by Vincent_M 2 replies 1.3k views
Original Post
Vincent_M
Vincent_M
I have a generic "Object" class that is my basic scene node for my scene manager. My scene manager manages certain subclasses of the Object class such as my "Camera", "PointLight", and "SpotLight" classes. The Object class contains the core functionality for managing it's own model transformation matrix which can also be attached to other transformation matrices. Due to this, Object has methods such as "Identity()", "Translate()", and "Rotate()", and "Scale()". Now, certain subclasses won't require the "Scale()" method such as my Camera, PointLight, or SpotLight classes.

When inheriting from these classes in C++, is there a way to remove access to this member function? All of these members are virtual, so what I do is override them with protected methods that literally do nothing. Is this a good practice?
Ashaman73
Ashaman73

I have a generic "Object" class that is my basic scene node for my scene manager. My scene manager manages certain subclasses of the Object class such as my "Camera", "PointLight", and "SpotLight" classes. The Object class contains the core functionality for managing it's own model transformation matrix which can also be attached to other transformation matrices. Due to this, Object has methods such as "Identity()", "Translate()", and "Rotate()", and "Scale()". Now, certain subclasses won't require the "Scale()" method such as my Camera, PointLight, or SpotLight classes.

When inheriting from these classes in C++, is there a way to remove access to this member function? All of these members are virtual, so what I do is override them with protected methods that literally do nothing. Is this a good practice?

There's no way to do it at compiler time . But you could either overwrite the method with a dummy implementation (do nothing when scaling) or you could throw an error/exception when accessing one of this methods.In the latter case it is important to know the context in which you call the scale methods.

An other option would be to extract the scale method and put it in its own class, like ScaleableObject extends Object. In this case Camera/Pointlight would inherit from Object, instead of ScaleableObject.
_the_phantom_
_the_phantom_
You are trying to make your 'base' object do too much and violating the Single Responsibility Principle which is making your life harder than it needs be,

You say it is a 'node for your scene manager' and yet everything else extends off it as well.

Your scene manager should do just that; manage nodes in a scene. These nodes can link to other objects but they shouldn't BE those other objects.
Zahlman
Zahlman
1) Do you really need a PointLight? Isn't it just a SpotLight that happens to be of zero size?

2) Is a Camera really an Object? If an Object can be translated, scaled etc., then that implies that what you call an "object" is something that's part of the world, i.e. that have a physical representation in the world space. Does the Camera need a physical representation in world space? You will never Render() the Camera, right? So why would it? It doesn't need any kind of data relating to its shape. It just needs a position and orientation and FOV angle. It's not really the same kind of thing.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.