iDevGames Forums

Full Version: C++ Different forms of the same thing
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
For example If I wanted to write a complex number class, and of course I want to deal with normal and polar forms.
I've got a list of functions I can do in normal form, and another list I can do in polar form.

I would want it to call the function if it can in the form its currently in, but convert it to the other form and call the function if it can't.

so that means I suppose ill want ComplexNumber, and then PolarComplexNumber which is a subclass of ComplexNumber, as would be NormalComplexNumber.
but how can my instance of ComplexNumber (which is either a PolarComplexNumber or NormalComplexNumber) know what functions it can and cant call?
How can ComplexNumber convert itself?
What other ways could there be to do this?
Have you considered using templates?

I'm assuming you know about polymorphism using virtual methods already.
If you just want complex numbers, you could use the built-in support for them Rasp

Code:
#include <complex>

complex<float> var;

Jones

OneSadCookie Wrote:If you just want complex numbers, you could use the built-in support for them Rasp

Code:
#include <complex>

complex<float> var;

What exactly makes a number "complex"? Are we talking scientific notation or power expressions?
I can't see a reason to support both forms. Normal and polar forms of complex numbers are the same thing, just different representations. There exist no complex numbers that can be represented in one form but not the other.

A better solution, I think, is to have only one internal representation, but have conversion functions to / from the other representation.

Where it's easier to perform calculations in the other form you can convert to the alternate form at the beginning of the function and convert back when the calculations are done.

No subclassing needed, no typecasting, just one class.

The best solution is still simply using <complex>.
it a hypothetical example because I wanted to understand C++ class structure better Mad
unknown Wrote:I wanted to understand C++ class structure better Mad

What's so difficult about it? It shouldn't be any harder than understanding ObjC classes, only there are way more caveats in C++.
Reference URL's