Design choices in c++

Started by
5 comments, last by __fold 20 years, 7 months ago
I''m a bit curious. If you would write a simple math library and you want it to contain vectors, matrices and so on. From an oo-perspective, this would give a vector-class with scalarmul, dot- and crossproduct. A matrix-class with multiplications and so on. Is this how it''s done? And if so, how would you from class c access values in class Vector? Would you make the element''s public?
Advertisement
OO fundamentalists would say make them protected.

My math library has data members public. How many times can you write SetX(), GetX() without putting your head thru the screen ?

I found typedeffing the ''real'' type was enough.

i.e. typedef float REAL;
With matrices and vectors, I would make their members public, since they''re plain ol'' data types.

Go one step further with your design and use templates (that way the actual data type can be chosen by the user).

Regards,
Jeff
quote:Original post by __fold
I''m a bit curious. If you would write a simple math library and you want it to contain vectors, matrices and so on. From an oo-perspective, this would give a vector-class with scalarmul, dot- and crossproduct. A matrix-class with multiplications and so on. Is this how it''s done?

Regardless of what you''re developing, the design is driven by the requirements. You don''t appear to have any requirements beyond `write a math library''.
quote:Original post by Anonymous Poster
OO fundamentalists would say make them protected.

Experienced OO designers would not recommend that. Make them private.
quote:
My math library has data members public. How many times can you write SetX(), GetX() without putting your head thru the screen ?

Why are your programs wanting to get at the internals of your class so often? Perhaps your class is missing some functionality.
Depends on what it is.

If I''m writing a Rect class, then a Rect doesn''t itself *do* anything, it just clumps the data together. Hence it is a struct and I just stop there.

SabreMan is just suggesting that perhaps your Vector class needs to do more. Personally I would still leave them as public, but that is merely my opinion.

C++ cries out for properties in cases like this. I am torn about whether to use them and #define them to each compiler''s extension, but I don''t think GCC supports them.[/threadHijack]
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
This was exactly the kind of response I was hoping for.

Now, if I make a bounding volume. A sphere for example. Then, I need to access the radius and the origo from other classes. Now it''s not that obvious to make them public is it?

The reason I''ve asked the first question was that I''ve seen some Vector-classes that have public x,y and z-values and I thought it was ok. But the more I think of it. A vector is not a class, it''s just data. Shouldn''t all vector classes and similar just have functions that takes this data and calculate for example a crossproduct. I would never do a wrapper class around an int or a float. Why do it around vectors? Besides for the use of operator overloading. What do you think?
It doesn''t feel right to have a design were objects are constantly using other objects private parts.
quote:Original post by __fold
Now, if I make a bounding volume. A sphere for example. Then, I need to access the radius and the origo from other classes. Now it''s not that obvious to make them public is it?

The question is what are those other classes going to do with that information? It might be that the behaviour which requires the radius and origin should be within the sphere class. If that''s the case, then you might be replicating that same behaviour in many places across your project when you should be localising the behaviour within the class.

A further problem is that, by making the data members public, it is very easy to put the object into an invalid state. Suppose you have a square and you want to double its size. If you make the data members public, you don''t have an atomic operation for changing the size, so at some point you will violate the class invariants (if you double x and double y, or vice-versa, then inbetween the two operations the square is not a square, and can be observed to be so).
quote:
The reason I''ve asked the first question was that I''ve seen some Vector-classes that have public x,y and z-values and I thought it was ok. But the more I think of it. A vector is not a class, it''s just data.

That''s because you''re missing a usage context. Once you understand the association between the vector and some environment, it should become clearer what behaviours the class should exhibit.
quote:
I would never do a wrapper class around an int or a float.

Really? I''ve done it lots of times. What about if you want to assert additional invariants over the primitive type, such as having a restricted set of valid values? It all depends on how you plan to use a type.

This topic is closed to new replies.

Advertisement