Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning 3 values from a class??

Status
Not open for further replies.

dnnymak

MIS
Sep 19, 2004
9
US
I am trying to create a class that does a Dot-product; it returns an x, y and z value for a vector. I have implemented it so that I have to call it 3 times to get each value. Very inefficient!

Can I return an array from a class? I have been able to read an array into my class, but can't figure out how to write out to an array. Any recommendations would be greatly appreciated!

Thanks,
dnnymak
 
You cannot return an array, because an array is really just a pointer to a chunk of memory that would go out of scope as soon as the function returns. You could allocate it with new, return the pointer, and then delete it later, but I don't think I need to explain why that is a terrible idea. What you should do is just make a class for the vector. This will make things much cleaner than using arrays, and you can overload operators such as +, -, * for scalar multiplication, etc, then make other functions for dot and cross products as needed. If you want to do this yourself go ahead - it is not difficult. I have made my own class for this for several projects and it works quite nicely, I'd be happy to email it to you if you want it.

Just curious, a dot product returns a scalar, so I don't know why you need to return an array anyway...
 
Rereading your post, it sounds like you are confused as to what a class is, since you are describing a function but keep calling it a class. If you have that confused just say so and we can explain what a class really is.
 
Sorry about the confusion, I was using that as an example (I meant cross product). Anyhow, I am taking a computer graphics class and am doing some ray tracing. Therefore I am doing a lot of vector math and am trying to keep it portable by creating classes to do some of the work. I am fairly new at C++.

I am looking for an easier way to manipulate vectors without to doing it 3 times( x,y,z ). Could you expand upon the idea of a class vector? I will need to use the vector to create other vectors, find angles, check for intersections ext…

Thanks,
Dnnymak
 
Maybe I dont know what a class is. I have been creating classes like functions, which has been a lot of work. I guess a class should be a collection of functions, like a car has wheels, brakes ... I think I am trying to make my car class out of a bunch of little classes. Does that make sense?
 
Here's a basic idea of what to do:

class CVector
{
public:
double x,y,z;

// lots of overloaded operators here
void operator += (CVector& v)
{
x += v.x;
y += v.y;
z += v.z;
}

void operator -= (CVector& v)
{
x -= v.x;
y -= v.y;
z -= v.z;
}
};

void CrossProduct(CVector& answer, CVector& v1, CVector& v2)
{
answer.x = v1.y*v2.z - v1.z*v2.y;
answer.y = v1.z*v2.x - v1.x*v2.z;
answer.z = v1.x*v2.y - v1.y*v2.x;
}

I've done ray tracing before - it's really cool but it can be incredibly slow (mine had unlimited light sources, reflection, refraction, and all kinds of other neat effects), so I would recommend implementing functions like cross product as shown, taking all input vectors as references and the output as a parameter to the function. This saves a small amount of time just copying data. Also, inlining as many things as possible could help.

Some people would recommend overloading * operator for dot or cross product, but that has always seemed ambiguous to me, so I'd say just use it for scalar multiplication and make global functions DotProduct and CrossProduct do them.

Hope this helps.
 
A class describes a noun (e.g. Car, Account, Rectangle).

A function describes a verb (e.g. Sort, Remove, Draw).


> I guess a class should be a collection of functions, like
> a car has wheels, brakes

Now, it seems you're confused about what a function is!

"Wheel" and "Brake" are still nouns. Those would be represented by classes, as well.

A Car might have "Drive" and "Stop" functions, and those functions might utilize the Wheel and Brake objects of the Car.


A class should be a set of data and the operations that can be performed on it.


> I think I am trying to make my car class out of a bunch
> of little classes. Does that make sense?

It's fine and good and normal for an object of one class to contain objects of another class.
 
Oh, and for the original problem, return a:
Code:
struct Coords {
    double x;
    double y;
    double z;
};

Or else return an std::vector<double> with 3 elements.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top