Using the [] operator overloading on a class.

Started by
28 comments, last by Roof Top Pew Wee 20 years, 9 months ago
Say I have the following:



template <class cType> class dArray
{public:

   cType arrayVariable[1024];

	cType operator[](int numToReturn)
	{
		return arrayVariable[numToReturn];
	}

}

class enemyPlane
{public:
   float f;

}

dArray<enemyPlane> ep;

// later, why can't I do this, assuming the variable is all created:


ep[0].f = 2.3f;

This isn't my exact code, but basically that's what I'm trying to do. Can you guys see what's wrong here? More specifically, I get error C2106: '=' : left operand must be l-value --Vic-- The future of 2D game development: Flat Red Ball
Advertisement
Your operator returns a value which is an r-value, when you want to return a reference.
Well, you prob get what I''m trying to do, but I have no idea how to return a l value. Could someone help me out on that?


--Vic--

The future of 2D game development:
Flat Red Ball
I think the problem lies here:

cType operator[](int numToReturn) 


When this function returns, the value of whatever happens to be in arrayVariable[numToReturn] is returned.

What you want is the ability to modify stuff in that array from the looks of it. So just have it return by reference, something like this:

cType & operator[](int numToReturn)




You fight like a dairy farmer.

--{You fight like a dairy farmer!}

cType& operator[](int numToReturn){   return arrayVariable[numToReturn];}

It now returns a reference, which is a valid l-value.
You want to return a refernce.
What your [] operator is doing is returning a temporary copy of arrayVariable[numToReturn].

You want
cType &operator[](int numToReturn)
It is also advisable to overload the method, so that constant objects of your class can use the operator:
template <class cTyoe>class dArray{public:    ...    cType& operator[](int n)  { return arrayVariable[n]; }    const cType& operator[](int n) const  { return arrayVariable[n]; }    ...private:    ...    cType arrayVariable[1024];    ...};

Now you can do things like so (assuming you have overloaded this sort of constructor):

float some_floats[100] = { ... };

...

const dArray<float> float_array( some_floats, 100 );

std::cout << float_array[0] << float_array[46] << float_array[82] << std::endl;

Whereas before you wouldn't have been able to invoke the index operator (operator[]()), as float_array is a constant object and the index operator wouldn't have guaranteed to not alter the object's members, and so it would not have been allowed to be invoked. Because of the keyword const at the end of the new overloaded method's definition, the user can index into the array type, as the method promises not to alter the object's contents. Have a look on Google if you don't understand.

Also, you might want to read up about the STL containers, std::vector inparticular, to give you some better ideas about implementing what I think that you are trying to do.

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on July 4, 2003 8:59:48 AM]
[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || [email=lektrix@barrysworld.com]E-Mail Me[/email] ]
Well you question has been answeared in full - but it does worry me that you have no bounds checking.. ie your program would most likely crash horribly with this example ( or even worse just give you memory outside the array)

ep[1024] = new enemyPlane();

it may offcourse be that you just cut it to make the code smaller when posting here, how ever I thougth I''d give you an heads up...



/Please excuse my bad spelling - My native language is binary not english
|Visit me
\Take my advice - I don''''t use it...
/Please excuse my bad spelling - My native language is binary not english|Visit meTake my advice - I don''t use it...
quote:Original post by guppy
but it does worry me that you have no bounds checking.. ie your program would most likely crash horribly
Bound checking can be easily done with mod operator (%).
return arrayVariable[numToReturn % MAX_ARRAY];
quote:ep[1024] = new enemyPlane();

But ep[1024] does not return a pointer to enemyPlane. I think Roof Top''s array class is for read only storage. So, it is safe.
quote:Original post by alnite
quote:Original post by guppy
but it does worry me that you have no bounds checking.. ie your program would most likely crash horribly
Bound checking can be easily done with mod operator (%).


I''d rather have my program crash than wrap indices like that.

Why you shouldn''t use iostream.h - ever! | A Good free online C++ book

This topic is closed to new replies.

Advertisement