| | |
class design question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 638
Reputation:
Solved Threads: 46
I have a base class called "ModelFile" which I derive several types of 3D model file classes from.
Many of these types of files are just a set of points, so in the base class I have a vector<Point> member. Some of the derived file classes also have vector<Triangle> members. The problem is, in a class with triangles, if I move the points, I need to update the triangles (even if the triangles have their 3 points stored as the address of the points, I still need to recalculate normals). I thought of two solutions:
1) put the vector<Triangle> in the base class so I can call an UpdateTriangles() function every time a MovePoints() function is called (which is in the base class). The vector<Triangle> would just be empty for derived classes without triangles and therefore the update triangle function would have nothing to do in those cases.
2) is there a way to somehow flag/trigger something to happen in particular derived classes? ie when MovePoints() is called, at the end of the function say "if the current class is a derived class with triangles, do soemthing"?
Any suggestions are appreciated!
Thanks,
Dave
Many of these types of files are just a set of points, so in the base class I have a vector<Point> member. Some of the derived file classes also have vector<Triangle> members. The problem is, in a class with triangles, if I move the points, I need to update the triangles (even if the triangles have their 3 points stored as the address of the points, I still need to recalculate normals). I thought of two solutions:
1) put the vector<Triangle> in the base class so I can call an UpdateTriangles() function every time a MovePoints() function is called (which is in the base class). The vector<Triangle> would just be empty for derived classes without triangles and therefore the update triangle function would have nothing to do in those cases.
2) is there a way to somehow flag/trigger something to happen in particular derived classes? ie when MovePoints() is called, at the end of the function say "if the current class is a derived class with triangles, do soemthing"?
Any suggestions are appreciated!
Thanks,
Dave
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
Don't use the first option. The whole point of a Base class is capturing functionality that is common to all derived classes.
As to the second option, there are many ways. Such as....
Provide a protected virtual function named UpdatePoints() in your base class. Implement it to do nothing (i.e. a body with nothing in it). Call it from your MovePoints() function, preferably after updating the vector<Point> member.
Override it in the derived class as appropriate (eg in your class with triangles, use it to update the vector<Triangle> based on the updated points).
Making it protected simply ensures it can't be called outside the class hierarchy (as there's probably no point in that). That's optional, depending on whether you want to allow it to be called by arbitrary code, obviously.
As to the second option, there are many ways. Such as....
Provide a protected virtual function named UpdatePoints() in your base class. Implement it to do nothing (i.e. a body with nothing in it). Call it from your MovePoints() function, preferably after updating the vector<Point> member.
Override it in the derived class as appropriate (eg in your class with triangles, use it to update the vector<Triangle> based on the updated points).
Making it protected simply ensures it can't be called outside the class hierarchy (as there's probably no point in that). That's optional, depending on whether you want to allow it to be called by arbitrary code, obviously.
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
If there are common things that all classes which handle triangles have to do, put those functions into their own class (eg TriangleModelFile derived from ModelFile). Derive the specific/multiple classes (eg SpecificTriangleModelFile) from that generic triangle-handling class, and give it specific behaviour it requires.
It's often a good idea to make base classes are abstract (i.e. you can't instantiate them) and derived classes are concrete (i.e. they can be instantiated). That way you reduce the temptation to move things to a base class that are only needed by some derived classes.
It's often a good idea to make base classes are abstract (i.e. you can't instantiate them) and derived classes are concrete (i.e. they can be instantiated). That way you reduce the temptation to move things to a base class that are only needed by some derived classes.
![]() |
Similar Threads
- To Degree or not to Degree - that is my question (IT Professionals' Lounge)
- Help with Class (C++)
- Java class design question. (Java)
- Executable program design question.... (Java)
- EASY question? checking for line feed (return) in a html file with C++ (C++)
Other Threads in the C++ Forum
- Previous Thread: OpenGL? Graphics? Help please
- Next Thread: C++ Zeller's Algorithm
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





