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.
MyType MyClass::getMyType() { return mMyType; }
const MyType& MyClass::getMyType() { return mMyType; }
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
.
The return type of getter 'loading' is 'dynamic' which isn't a subtype of the type 'bool' of its setter 'loading'.
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.
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.
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 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; }
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.
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.
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.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With