Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Roof Top Pew Wee

Using the [] operator overloading on a class.

This topic is 5583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

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

It now returns a reference, which is a valid l-value.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!