Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should a C++ getter return

Tags:

c++

getter

What is the best practice for a C++ getter method which is supposed to return a non trivial type, but a member which is of type class, or struct.

  1. Return by value, such as: MyType MyClass::getMyType() { return mMyType; }
  2. Return by const reference: const MyType& MyClass::getMyType() { return mMyType; }
  3. Return by address: MyType* MyClass::getMyType() { return &mMyType; }

where

class MyType { /* ... */ };

class MyClass
{
  private:
     MyType mMyType;
}

I specifically worry about the following usages of this method. Can you please elaborate in details how this might affect copying the object, and the danger of dangling references and wild gone pointers if function() wants to save it for further usage.

MyType* savedPointer;

SomeType function(MyType* pointer) { savedPointer = pointer; };

a. valid for 1. and 2.

{
  MyType t = myClass.getMyType();
  function(&t);
}

// is savedPointer still valid here?

b. valid for 1. and 2.

{
  const MyType& t = myClass.getMyType();
  function(&t);
}

// is savedPointer still valid here?

c. valid for 1. and 2.

{
  MyType& t = myClass.getMyType();
  function(&t);
}

// is savedPointer still valid here?

d. valid for 3.

{
  MyType* t = myClass.getMyType();
  function(t);
}

// is savedPointer still valid here?

where myClass is an object of type MyClass.

like image 491
Ferenc Deak Avatar asked Nov 20 '13 08:11

Ferenc Deak


People also ask

What is the default return type of getter?

The return type of getter 'loading' is 'dynamic' which isn't a subtype of the type 'bool' of its setter 'loading'.

Does a getter have a return type?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value.

What is getter in C?

The getter function is used to retrieve the variable value and the setter function is used to set the variable value. Remember: You can directly access public member variables, but private member variables are not accessible. Therefore, we need getter functions.

Why do we make getter Const?

This is a a constant function, meaning a function that will not alter any member variables of the class it belongs to. This is the style recommended to use for getters, since their only purpose is to retrieve data and should not modify anything in the process.

What is the best practice for a C++ getter method?

What is the best practice for a C++ getter method which is supposed to return a non trivial type, but a member which is of type class, or struct. Return by value, such as: MyType MyClass::getMyType () { return mMyType; } Return by const reference: const MyType& MyClass::getMyType () { return mMyType; }

What is the use of Getter and setter in C++?

Getter and setter functions allow access to the private data in a safe mode. As the setter function C++ is used along with the data validation, and checks whether the user enters a valid value or not. In some cases, you can not use getter and setter functions. If you retrieve a member function but can not set the value, then it is read-only.

Is a getter allowed to return a different value than is stored?

Yes, if it is warranted. However, for a reference type, you should really be storing the default value instead of merely returning it. Otherwise, it may create unexpected behavior. Is a getter allowed to return a different value than is stored? Yes, if it is warranted. Is this current example a situation where you need to do this? No.

What is the purpose of a getter?

The purpose of having a getter is to access the stored information, not to modify it. By having the getter function return a const reference you ensure that the object's state can be only modified through setters. This gives you some really nice compile time optimizations and makes easier to catch some bugs.


2 Answers

You can provide both const and non-const versions:

MyType       & MyClass::getMyType()       { return mMyType; }
MyType const & MyClass::getMyType() const { return mMyType; }

I wouldn't provide a pointer version, since that implies that the return value might be the null pointer, which it can never be in this instance.

The real point, however, is that you are basically giving the caller direct access to the internal object. If this is your intent, then you may as well make the data member public. If it isn't, then you will need to work harder to hide the object.

One option is to retain the MyType const & accessor, but provide more indirect means to modify the internal object (setMyType(…) or something more tailored to the semantics that you are trying to express at the level of the containing class).

like image 70
Marcelo Cantos Avatar answered Oct 21 '22 10:10

Marcelo Cantos


In general, you should prefer return by value, unless you explicitly want to guarantee that the reference will designate a member (which exposes part of your implementation, but is desirable in cases like std::vector<>::operator[]). Returning a reference prevents later changes in class, since it means that you cannot return a calculated value. (This is especially important if the class is designed to be a base class, since returning a reference creates this restriction for all derived classes.)

The only time you should return by pointer is if a lookup or something is involved, which may return in having to return a null pointer.

Returning a reference to const may be a valid optimization, if the profiler indicates performance problems here, and the call site can also deal with a const reference (no modification of the returned value, no problems with lifetime of object). It must be weighed against the additional constraints on the implementation, of course, but in some cases, it is justified.

like image 24
James Kanze Avatar answered Oct 21 '22 08:10

James Kanze