-
Overloading
I know you can create a list simply with the type list provided by c++ but i need to do a list by using a vector. I think what i am asking is possible but i have no idea how to do it ..
so far i just overloaded [] and = operators so that [] returns the value of the list in the position given and = replaces all elements in the list that are equal to number_to_replace (which is global) with the number next to = .
Code:
#include <iostream>
#include <algorithm>
#include <vector>
//Global variable
int number_to_replace;
class fifo {
vector <int> integervector;
void operator=(int replace_with){
replace(integervector.begin(), integervector.end(), number_to_replace, replace_with);
}
int operator[](int number){
if (integervector.size()>=number)
return integervector[number-1];
else
return -1;
}
};
int main(){
int list_position=0;
//Making an object of type fifo
fifo fifolist;
//Is this possible with overloading ?
//fifolist[list_position]=6
return 0;
}
Any help will be much appreciated (again) ! :)
-
I am not sure of what you are asking make a list of vector what do you mean.
[edit]Oh I think I see what you are talking about. That would look something like this
Code:
int &operator[](const int index)
{
return theNumber[index];
}
[/edit]
-
my bad i just wrote the code quickly here,
i want fifolist[list_position]=6; to replace the element at the list position with 6 and i don't want to use the declaration list fifo ;.
-
Read the edit above. I understood what you meant afterall
-
excuse my n00bness but what is thenumber .. ?
-
That would be the vector of int's name. I just made it up for the sake being. The code would look something like this for your class.
Code:
int &operator[](int number)
{
return integervector[number];
}
You would need to define another function if you wanted to do bounds checking. The vector uses the at() function for bounds checking operator[] just acts like a plan ol array as it should IMO.
-
It's the vector.
Lose the if statement in your operator[]. Why would you want to return a -1 if the index isn't in bounds? What if the particular index contains the number -1. How do you know if it's in bounds or not? If anything, have it return 0. Personally, I like to return the nearest index. Meaning if the index is below 0, it returns the 0 index, if it's above the highest index, it returns the highest index. You could also throw an exception or write something to cerr to let you know what happened.
[EDIT]Whoops, beaten to it[/EDIT]
-
the -1 was just there to tell me if there is no such position in the vector yet because it's the user that gives the position in my normal program.
So all the difference from what i did is & before operator ???
-
Yes but there is deeper meaning to & the & is the reference operator(in this case). Which means it returns a reference to the specified position. Which means to you that you can modify the data at this postion. Going further on what I wrote about above. You would not be able to return -1 from that function because you can't reference -1 because it is just a value not a variable. So you have two options either a)Check bounds before hand. b)Make a bounds checking function but this would only be for read only access.
-
thanks man , that's exactly what i needed !! *sm00tch*
-
Haha... he kissed you prog.
-
Hey you have tried to kiss me on more than one occasion Sly. Stop trying to cover your jealousy with humor.
-
You could also return integervector.at(number-1) and let the vector do the range checking for you.
-
You could get the size of the vector and automatically reduce the passed offset. So, if I passed -1, I'd get the last element...