# Defining a member function that returns a reference

This topic is 2255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi folks,

i have a base class shown below:
 class CEntity { public: CEntity(); virtual ~CEntity(); const TVector<float, 3>& CEntity::GetVector(); }; 

you will notice a virtual function GetVector(). Although the base class has no member variable TVector, i am declaring this virtual function anyway to be used by derived classes which may have TVector variables as members.

The problem is the base class have no TVector member to return to when defining GetVector() , so this is how i defined this function in my base class:

 const TVector<float, 3>& CEntity::GetVector( long nType ) { TVector<float, 3> *T = 0; return *T; } 

It works, or at least the compiler does not complain. Also, since the base class won't have any TVector variable i do not see any risk to this (or probably i have not discovered any yet).
However, i believe it is illegal to do this. Therefore i would assume this is a the best solution out there. So my question is how should i best define the GetVector() function?

##### Share on other sites
A valid reference must always refer to a valid object or the program is not well defined. If you have a base class that don't have anything valid to return, then you must ensure that the derived classes do. Make the function pure virtual to ensure that every derived class has to provide a function that actually returns something. As a result of being a pure virtual function, you don't have to implement it in the base class.
[source]
class CEntity
{
public:
CEntity();
virtual ~CEntity();
virtual const TVector<float, 3>& GetVector() = 0;
};
[/source]
If you expect cases when no vector can be returned, then you need to return something that can reflect the absence of a value. For example, return a pointer instead of a reference, or return a boost::optional<TVector<float, 3>>.

##### Share on other sites

A valid reference must always refer to a valid object or the program is not well defined. If you have a base class that don't have anything valid to return, then you must ensure that the derived classes do. Make the function pure virtual to ensure that every derived class has to provide a function that actually returns something. As a result of being a pure virtual function, you don't have to implement it in the base class.
[source]
class CEntity
{
public:
CEntity();
virtual ~CEntity();
virtual const TVector<float, 3>& GetVector() = 0;
};
[/source]
If you expect cases when no vector can be returned, then you need to return something that can reflect the absence of a value. For example, return a pointer instead of a reference, or return a boost::optional<TVector<float, 3>>.

Thanks for the reply Brother Bob, and the detailed explanation.
I never used boost before. perhaps this is a good time to explore this library.

Can i clarify with you when you say "return a pointer instead of reference". do you mean i change my GetVector() function to this?

 virtual const TVector<float, 3>* GetVector() 

##### Share on other sites
Yes, that is correct.

##### Share on other sites

Yes, that is correct.

I guess i am off to exploring boost library. i never used it before, so this is probably a good opportunity to learn using it.
Thanks a lot!

• 10
• 9
• 48
• 12
• 10
• ### Forum Statistics

• Total Topics
631385
• Total Posts
2999704
×