Thread: Overloading

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    27

    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) !
    Last edited by neoragexxx; 05-01-2006 at 04:28 AM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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]
    Last edited by prog-bman; 05-01-2006 at 04:31 AM.
    Woop?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    27
    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 ;.

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Read the edit above. I understood what you meant afterall
    Woop?

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    27
    excuse my n00bness but what is thenumber .. ?

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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.
    Last edited by prog-bman; 05-01-2006 at 04:47 AM.
    Woop?

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    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]
    Last edited by SlyMaelstrom; 05-01-2006 at 04:44 AM.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    27
    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 ???

  9. #9
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    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.
    Woop?

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    27
    thanks man , that's exactly what i needed !! *sm00tch*

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Haha... he kissed you prog.
    Sent from my iPadŽ

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Hey you have tried to kiss me on more than one occasion Sly. Stop trying to cover your jealousy with humor.
    Woop?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You could also return integervector.at(number-1) and let the vector do the range checking for you.

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    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...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  3. overloading operator problems
    By almich in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2004, 04:10 PM
  4. operator overloading
    By blue_gene in forum C++ Programming
    Replies: 6
    Last Post: 04-29-2004, 04:06 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 08:49 PM