# Class operation...

## Recommended Posts

zix99    205
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 Resize(int size);

void RemoveItem(int index);

int *items; //containing structure
private:
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;


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

##### Share on other sites
Morpheus011    309
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 [i] 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 on other sites
zix99    205
Yep, that did the trick, thanks :)

~zix~

##### Share on other sites
snk_kid    1312
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 on other sites
zix99    205
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 on other sites
Morpheus011    309
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 on other sites
snk_kid    1312
Quote:
 Original post by Morpheus011Vectors 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.

## Create an account

Register a new account