new operator, and assignment c++

Started by
7 comments, last by Evil Steve 15 years, 9 months ago
I have an array of type bcNORMAL, which is just to hold x,y,z. There is a function that outputs a normal to this array, but the assignment somehow fails. After checking the array, I find it holds no new value. bcNORMAL *SURFACE = new bcNORMAL[ size_x * size_y * 2 ]; SURFACE[i++] = GetFaceNormal( bcPOINT1, bcPOINT2, bcPOINT3 ); Any ideas on what this could be?
Advertisement
Quote:Original post by VprMatrix89
I have an array of type bcNORMAL, which is just to hold x,y,z. There is a function that outputs a normal to this array, but the assignment somehow fails. After checking the array, I find it holds no new value.

bcNORMAL *SURFACE = new bcNORMAL[ size_x * size_y * 2 ];

SURFACE[i++] =

GetFaceNormal(
bcPOINT1,
bcPOINT2,
bcPOINT3
);

Any ideas on what this could be?


What array? What do you mean it has "no new value"? What is thee value of i?
I overloaded the type to copy the values, and it shows its values as -4.3160208e+008, the default value.. I'm pretty sure of this, because I checked later when the stored values are copied to a Vertex array and they hold the same value. ALso, the light has all positive values, which explains why the -4 normal works. When rotating, the whole surface goes dark at once.
The value of i starts at 0, and the normal function goes like this, it returns the same type that is being copied to of course..

bcNORMAL GetFaceNormal(bcPOINT a, bcPOINT b, bcPOINT c){

bcVECTOR v1 = b - a;
bcVECTOR v2 = c - a;
bcNORMAL Normal;
Normal.x = ((v1.y * v2.z) - (v1.z * v2.y));
Normal.y = ((v1.z * v2.x) - (v1.x * v2.z));
Normal.z = ((v1.x * v2.y) - (v1.y * v2.x));
//((2×6 - 3×5), (3×4 - 1×6), (1×5 - 2×4))

return Normal;
}

Now I checked this function, and it definitely returns legitimate values, but they just are not copied at all it seems.
the VECTOR POINT and NORMAL are all the same typedefs to distinguish.
How did you overload operator=()? Can we see your implementation?
I think its definitely the overloading thats faulting here.. I tried this and it failed. I'm no expert at overloading :S
SURFACE[0] = bcNORMAL ( 0.5,0.5,0.5 );



bcPOINT bcPOINT::operator= (bcPOINT param) {
bcPOINT temp;
temp.x = param.x;
temp.y = param.y;
temp.z = param.z;
return (temp);
}
You're assigning values to the object that you're returning, but you're not doing anything to the object that you're actually working with. Try this instead


const bcPOINT& bcPOINT::operator= (const bcPOINT& param)
{
x = param.x;
y = param.y;
z = param.z;
return *this;
}
Quote:Original post by VprMatrix89
I think its definitely the overloading thats faulting here.. I tried this and it failed. I'm no expert at overloading :S
SURFACE[0] = bcNORMAL ( 0.5,0.5,0.5 );



bcPOINT bcPOINT::operator= (bcPOINT param) {
bcPOINT temp;
temp.x = param.x;
temp.y = param.y;
temp.z = param.z;
return (temp);
}


yes, indeed this is operator= fault. You want to "update" left operand, so you myst assign values to <this> pointer. Assigning to temp updates only local - automatic variable that is destroyed at the end of function (operator is just regular function (well, method actually). Also pass right operand by const ref so you will avoid unnecessary copy. So correct ver is:

bcPOINT bcPOINT::operator=(const bcPOINT& rhs)
{
x = rhs.x; //etc
return *this;
}

Ah it worked, should have known. Thanks
Quote:Original post by noe
bcPOINT bcPOINT::operator=(const bcPOINT& rhs)
{
x = rhs.x; //etc
return *this;
}
Actually, that should be:
bcPOINT& bcPOINT::operator=(const bcPOINT& rhs){  x = rhs.x; //etc  return *this;}

operator=() must return a reference to the object, not a copy.

This topic is closed to new replies.

Advertisement