Thread: const Something*& != Something*& const ?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    108

    const Something*& != Something*& const ?

    So, there's this bit of code:

    Code:
    class Package{
       public: string name;
    };
    
    int main(){
       set<Package*> packages;
    
       // do stuff here
    
       set<Package*>::iterator it;
       for(it = packages.begin(); it != packages.end(); it++){
          const Package*& moo = *it;
          cout << (*it)->name << endl;
       }
    
       return 0;
    }
    It doesn't compile.. but if you do typedef Package* Moo, and do const Moo& moo = *it, it works.

    Any way I can do this without going into typedefs?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Code:
    Package * const & moo = *it;
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    108
    that worked, thanks!

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    There's no need to use a reference there. Just copy the pointer out
    Code:
          Package* moo = *it;
          cout << moo->name << endl;
    Why would you be storing pointers in the set instead of objects? That makes it kind of hard to have any useful less-than operator to give it a desired ordering.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by iMalc View Post
    Why would you be storing pointers in the set instead of objects? That makes it kind of hard to have any useful less-than operator to give it a desired ordering.
    Suppose you have some sort of manager class which registers other objects. It is handy to store this registry as a std::set<Pointer>. Inserting, erasing, and searching for an object is O(log n).

    Suppose you want to hang some auxiliary information off of each instance of a given class, but you don't have the ability to alter the class definition. You can create a class AuxInfo and use a std::map<Pointer, AuxInfo> to associate data with each instance. Again, the O(log n) characteristics can be critical.

    Sometimes, using a sorted vector of pointers is better because of space efficiency, but it does make insert/erase more time-consuming. Lookup is still O(log n) though.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Yeah there's certainly times where it makes sense, but all too often people store a pointer in a container when it's not advantageous to do so.
    Just prodding to see if underthesun has a good reason.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM