storing references

This is a discussion on storing references within the C++ Programming forums, part of the General Programming Boards category; Hello Lets say I have the following data structure: Code: class object { public: void some_function(); std::string get_something(); void set_something(); ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    storing references

    Hello

    Lets say I have the following data structure:

    Code:
    class object {
    public:
      void some_function();
      std::string get_something();
      void set_something();
    private:
      //data
    };
    
    class main {
    public:
      main() : m_subclass(*this) { }
      const object& get_object() { return m_object; }
    
    private:
      object m_object;
      subclass m_subclass;
    };
    
    class subclass {
    public:
      subclass(main &obj) : m_object(obj.get_object()) { }
    
    private: 
     object &m_object;
    };
    This will give an error because object in subclass is not const.

    I've been told that its right to return references by const, but in that case I would have to store it in subclass as const object.. Do you guys store references by const / non-const or does it depend on situation?

    Thanks a lot for help!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by l2u View Post
    I've been told that its right to return references by const, but in that case I would have to store it in subclass as const object.. Do you guys store references by const / non-const or does it depend on situation?
    I only store a reference as non-const if I actually intend to modify it. Obviously there are situations where you can't use const, but the basic rule is to make it const unless that is impossible.

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by brewbuck View Post
    I only store a reference as non-const if I actually intend to modify it. Obviously there are situations where you can't use const, but the basic rule is to make it const unless that is impossible.
    Does having const object means I cannot call set_something() function by the const object reference?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,236
    Quote Originally Posted by l2u View Post
    Does having const object means I cannot call set_something() function by the const object reference?
    Yes. You can only invoke const functions through a const reference.

    If you need to call set_something(), then the reference will have to be held as non-const, which implies that it must be initialized from non-const, which implies that main::get_object() must return non-const, and so this decision percolates up through the entire design.

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by brewbuck View Post
    Yes. You can only invoke const functions through a const reference.

    If you need to call set_something(), then the reference will have to be held as non-const, which implies that it must be initialized from non-const, which implies that main::get_object() must return non-const, and so this decision percolates up through the entire design.
    So I should just store and return non-const object in case I want to be able to call non-const functions?

    Or you would go for some other design/approach?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    It's hard to comment on your design when you are posting toy examples (object, set_something, etc.). Can you post a real class that you are implementing this way?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler settings, references etc...
    By ejohns85 in forum C++ Programming
    Replies: 0
    Last Post: 05-14-2009, 04:53 AM
  2. References vs Pointer vs copying and storing stuff in classes
    By Monkeymagic in forum C++ Programming
    Replies: 20
    Last Post: 09-01-2006, 09:40 AM
  3. VC++ 2005 Express: missing references page
    By psychopath in forum Tech Board
    Replies: 1
    Last Post: 08-21-2006, 04:55 PM
  4. declare references to references works!
    By ManuelH in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2003, 07:14 AM
  5. Pointers and references...
    By SushiFugu in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 03:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21