C++ help, somebody?

MeanMeosh

Diamond Member
Apr 18, 2001
3,805
1
0
ok, a little layout... polynomial is a class, with two components, coefficientsPtr, a dynamic float array that contains the coefficients to the polynomial; and degree, an int number which is the degree of the polynomial.

i'm doing operator overloading here.

float& operator[] (int i); // the ith coefficient of f() is f
const float& operator[] (int i) const; // read-only version

are the function definitions.

for the first function, i just did

float& Polynomial::eek:perator[](int i)
{
return coefficientsPtr;
}

the second fuction is supposed to be a read only overload of []. what am i supposed to do different?

i swear, i've been working on this damn program for the past three days and dont know how to do it...
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
Originally posted by: MeanMeosh
ok, a little layout... polynomial is a class, with two components, coefficientsPtr, a dynamic float array that contains the coefficients to the polynomial; and degree, an int number which is the degree of the polynomial.

i'm doing operator overloading here.

float& operator[] (int i); // the ith coefficient of f() is f
const float& operator[] (int i) const; // read-only version

are the function definitions.

for the first function, i just did

float& Polynomial::eek:perator[](int i)
{
return coefficientsPtr;
}

the second fuction is supposed to be a read only overload of []. what am i supposed to do different?

i swear, i've been working on this damn program for the past three days and dont know how to do it...



when people say read only to me (in reference to programming) it usually means they want the keyword const so you cant modify the the return values. if you are returning a pointer you dont want the user to be able tom odify the the value that the pointer points to.