Quote:Original post by jregan
hi all, I have been following this thread along fairly nicely until around here. Ok, in this case of our vector class what is meant below when he says, "you did remember to use a typedef didn't you?" I am not sure what he is getting at.
If you had done something like
class Vector3f { float x, y, z; /* ... */ };
or just littered your code with
float vec[3];
Then you're SOL if you need doubles instead of floats. However, if you'd done
typedef float Scalar;
class Vector3f { Scalar x, y, z; /* ... */ };
or
typedef float Vector3f[3];
then you just change the float to double in the typedef and all is well.
Quote:
I have an additional question about the bird analogy. In that case of the penguin, doesn't that just mean it should have been a different class hierarchy in stead? I think someone else said it as well, the assumption if immediately placing penguin under bird in the class hierarchy is the mistake. What about:
Bird
/ Flying birds Non-flying Birds
/ Cardinal Penguin
Isn't that just fine? Just leave the virtual fly() method out of the bird base class?
You've missed the point. Yeah, if I see I'll someday need to consider flightless birds, then I'd just make the hierarchy you mentioned. But what if to start with I'm considering pigeons, robins, and cardinals? I'll be like "So, all the birds need a fly() method, I'll just put that in the base class". A year down the line I (or my replacement if I've moved onto bigger and better things) am told that we're now considering penguins, as well. What was a good design a year ago just turned sour.
In some cases, it's probably best to just make fly() a no-op on penguins. In others, it may be better to make it do the equivalent penguin thing. In others, restructuring the hierarchy is the best bet. But in all cases, the design that made sense at the beginning is causing trouble now. In more complicated cases, it'd be even harder to see a problem like this coming.
Quote:
I genuinely do wish to have some more information about using a typedef in this class declaration for vector that we're speaking of. I want to know because I think this touches on the same methods that can be used in other patterns such as creating an object factory.. if you wanted to make a vector using a generic object factory which is based off a template, where is the right place to use this typedef? we use all of these constructs as well in the commercial codebase that I work on which is not graphics, but financial software and in some ways I struggle to understand their use of these principles, mostly centered around dynamic memory allocation and creation for new objects. Sorry for the verbosity! Hope someone can help clarify!
I think what you need are straight up templates. Try google for a good tutorial. typedefs are a less powerful tool for simpler problems (like deciding at compile time whether to use floats, doubles, or long doubles).