Sign in to follow this  
giugio

[c++]Different operation & base class

Recommended Posts

giugio    246
Hy. I have an array of string that descrive the caracteristic of a class , a x,y and z int ,and in particulary a int nType that can be 0 or 1. I think to create a base class for these object (basemarker)and manage the creation/deletion of objects from the parsing of the list of string. If the nType(value in the single string of the array of string) is 0 i create an object from class MarkerMesh derived from basemarker and if the nType = 1 i create an object MarkerWall. I must push all the objects of the two type in a container like vector<basemarker> because any step of time string array change and the list of objects can grow or decrease. Each step of time i iterate the container for drawing meshes and create wall in opengl. For the meshes is simple i call a virtual method DrawMesh on each basemarker , then if the basemarker is a MarkerMesh the object draw itself data This is a usual problem that i'm not understand : these are the questions: 1)is correct to create in the base class all the virtual method that a derived object of type A or B can use? For ex: If the object is a MarkerWall not use the DrawMesh method , then not override the DrawMesh on the base class basemarker If the object is a MeshMarker else the class override the DrawMesh and draw itself(it's mesh data). This for me is not clean 2)What is a best solution? 3)What is a best solution with templates? 4)Anoter operation that i must do is the calculation of perimeter , but for do this i must have all the points of all the MarkerWall, then i must iterate all the objects in the container ,see if is a MarkerWall, call getPoint , then when i have all the points calculate the perimeter . in short this method don't satisfies me. thanks Thanks.

Share this post


Link to post
Share on other sites
jpetrie    13104
Do not bump your thread by posting and immediately deleting your post.

Your problem description and questions are not particularly clear. However:

It sounds like you want to use a factory for constructing appropriate subclasses of your base class based on data you read in at run time; this is a sane approach. Similarly, using virtual methods defined in the base class and implemented in the appropriate subclasses to customize the behavior is a sane approach.

The problems with what you have described so far seem to be:
(1) you should store pointers to the base class in the vector, otherwise you won't get polymorphic behavior and will in fact slice the subclass instances.
(2) you might be violating SRP -- doing too many things in one class (both the logic of the marker/tile type and its rendering).
(3) you don't need templates for this.

Share this post


Link to post
Share on other sites
giugio    246
Quote:
Do not bump your thread by posting and immediately deleting your post.

sorry.I'm not be repeated.

Quote:

(2) you might be violating SRP -- doing too many things in one class (both the logic of the marker/tile type and its rendering).

my personal problem is that when i think at an object i think at a box that do somethings.
The first idea that i have when you speak about SRP is to create anoter box and incapsulate it in a more big box(class)a mark container box.
I think alwais at the encapsulation ,and at the end at a container object(list,vector , map that contains all)that must be iterated but it isn't the right solution i suppose .

I write this because i'm still reading the book of the gof on patterns and the things appear me different.
How i can subdivide the responsability in many class withowt always encapsulate?
there are some rules?
sorry for the english and if my post appear so bad , but i must learn the oop.

thanks.
by

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this