Jump to content
  • Advertisement
Sign in to follow this  
zix99

Class operation...

This topic is 4770 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

Hi everyone, I'd do a search for this on google, but I have no idea what it's called, or even if it's possible. Basically, what I would like to do is take a class like this:
class INTLIST{
public:
	INTLIST();
	~INTLIST();

	void Setting_MakeAhead(int amount);

	void Unload();
	void Resize(int size);

	void AddItem(int n);
	void RemoveItem(int index);

	int *items; //containing structure
private:
	bool loaded;
	int make_ahead;
	int s_size; //structure size (structure)
	int u_size; //amount of structure being used (usage)
};


and have it when I call the class as a variable I get an item from the variable *items. IE:
INTLIST list;
int number;
number=list[5];  //instead of number=list.items[5]


Is this possible? On second glance it doesnt appear to be, but I'd like to ask nevertheless. Thanks ~zix~

Share this post


Link to post
Share on other sites
Advertisement
If i understand you correctly you are wanting to overload the [] operator so that instead of having to explicitly tell it that you want the lists 5th element it should implicitly understand that a means, find the i'th unit of my list? If this is the case then you simply need to overload the [] operator. you can google 'operator[]' and it will probably give you some good results. The overload should be fairly straight forward, but if you have trouble with it post googlization, feel free to ask some more questions.

Share this post


Link to post
Share on other sites
Yes it is possible, however it appears you want a vector ADT (a dynamic array) not a list and typically it would not be good practice to define subscript access for a list ADT.

Anyways the standard library provides a set of containers such as vectors, lists etc and i think you really want the standard library vector:


#include <vector>

//...
std::vector<int> vints; // a vector of ints
//....
int foo = vints[5];


To find out more check here

Share this post


Link to post
Share on other sites
I dislike vector's for some reason. I have used them, but I just don't like them, and I'm not sure why. I suppose I would be better off using a standard than a home-made library, but I like making my own stuff rather than using pre-made libraries. I guess this is more of a learning experience than the most efficient way, but nevertheless thanks.

~zix~

Share this post


Link to post
Share on other sites
nothing wrong with making your own stuff. Vectors do have a downside in that they are slow to remove and copy. However, I frickin' love vectors and use them all over the place (maybe a little too much, lol). Glad that helped tho.

Share this post


Link to post
Share on other sites
Quote:
Original post by Morpheus011
Vectors do have a downside in that they are slow to remove and copy.


I think you need to be a little more specific, if you meant removal & insertion at arbitrary positions in the vector (particularly in the middle) then yes it isn't very efficient for that task but this is the same for C-style dynamic arrays and doing it yourself has more chance of a naive implementation without thought. This is when you choose the wright container for the task/job which a dynamic array in this context probably is not.

About copying you need to be specific as i'm not sure what you mean by it. Again its probably a case of picking the wright container for the job or using it effectively for example using std::vector::reserve.

It should be noted that the standard library containers are parameterized by allocator type so you can provide custom allocators to them.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • 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!