   Reference, objects, const and other nonsense
  I am creating a class in C++, and using a "Has A" relationship with another class that I''ve designed.  My dilema arises from building a getter function that returns the private object.  From my (albeit somewhat rudimentary) knowledge of C++, I basically have three options:1) const MyClass& MyOtherClass2::GetMyObject() constThe problem with this arises when attempting to call member functions on the returned object in a manner such as this:cout << MyOtherObject.GetMyObject().GetName();This generates a compile error "cannot convert ''this'' pointer from ''const MyClass'' to ''MyClass&''".  The only way around this is to create a temporary local to store the object:MyClass& TempObject = MyOtherObject.GetMyObject();cout << TempObject.GetName();Not a bad option, but it does create some overhead and clutter the code a bit.2)  MyClass& MyOtherClass:GetMyObject() constThis option would allow the program to alter the object directly, in this manner:MyOtherObject().GetMyObject() = SomeOtherObject;Not particularly desireable, since it could allow our program to recklessly alter our nested object.3)  MyClass MyOtherClass::GetMyObject() constThis option allows the cleaner code and prevents the program from altering the object directly, but adds the extra overhead of passing the object by value.  This overhead could be quite significant, depending on the size of the object.Is there a cleaner solution to this dilema?  I am curious how others have worked around this.  If there is no cleaner solution, I would be interested in hearing further discussion of the pros and cons of each of the above solutions.  FYI, I am using VS C++ .NET. 
  Your first idea should work, as long as the functions that you''re calling on MyClass (the one that''s returned by the first method) are also flagged as const:class MyClass{  void foo() const { }};class OtherClass{  const MyClass &GetThing() { return myThing; }};Here, given an instance of OtherClass named oClass, you *should* be able to do: oClass.GetThing().foo()I didn''t double check this with VC.NET, but I imagine it works. ;  Indeed, you seem to be correct.  What if I need to call a non-const function on the returned object, however?  Am I then forced to copy to a temp? ;  You seem to have yet to learn the wonderful powers of the const keyword. Here''s I think what you want:#include <string>#include <iostream>class MyObject{   std::string Name;public:   MyObject( const std::string & SetName )   { Name = SetName;   }   const std::string & GetName( void ) const   { return Name;   }};class MyObjectManager{   MyObject Object; //we''re only going to manage one objectpublic:   MyObjectManager( const std::string & ObjName ) : Object ( ObjName ) {}   const MyObject & GetObject( void ) const {return Object;   }};int main ( int argc , char ** argv ){  MyObjectManager Manager("The Object");  std::cout << Manager.GetObject().GetName() << std::endl;}  The const after the argument list allows you to use the function, even when the object is ''const''.Compiles and runs using gcc on my linux machine. -Mike ;  Right, this is essentially using the technique provided by AP.  But what if I needed to call a non-const function, say SetName()?  Naturally I can''t declare SetName() as const, so if I write something like this: //in class declarationvoid SetName(const std::string& NewName){Name = NewName;}//...//This line causes an errorManager.GetMyObject().SetName("NewName");...I get a compliation error.  How do I allow the program to call non-const functions on the returned object? ;  If you think about it, you''re returninag a const object because you don''t want it to be changed. And now you want to change it.That should set off a few alarms that something is wrong.The obvious solution is to return a non-const reference. There is a keyword mutable that allows you to modify const objects, but I''ve never been able to get it to work. Somebody with better knowledge of C++ will have to chime in on that one. ;  Jackal, I see what you're saying.  I was nervous about allowing the program to alter the nested object freely using an equality operator (which is not declared for my particular class), but now that I think about it, that's probably not so bad.   [edited by - GD-Silver on February 2, 2004 10:35:09 PM];  The usual method for accessor functions that return references, pointers or iterators is to create two versions of an accessor function: one that''s const and one that''s not const. (Unless it''s specifcally a read only accessor function.) For example, in std::vector you have something like:class vector {  // ... stuff ...  public:    // ... stuff ...    iterator  begin();    const_iterator begin() const;    // ... more stuff ...    reference  operator[](size_type n);    const_reference operator[](size_type n) const;    // ... even stuff ...}; ;  This is perfectly acceptable code: class Thingy {Other& get();const Other& get() const;};If the Thingy you get() is const then it will return a const reference to an Other, else you will get a non-const reference.EDIT: SiCrane beat me. *shakes fist in SiCrane's direction*   [edited by - Doc on February 2, 2004 11:16:04 PM];quote:This generates a compile error "cannot convert ''this'' pointer from ''const MyClass'' to ''MyClass&''". The only way around this is to create a temporary local to store the object:MyClass& TempObject = MyOtherObject.GetMyObject();cout << TempObject.GetName();I don''t even know how you got that to compile. You''re assigning a reference to a const to a reference to non-const, therefore ignoring the const keyword completely, which should be flagged as a compile time error. Is there something I''m missing here? 