Jump to content
  • Advertisement
Sign in to follow this  
GodFear

Defining a member function that returns a reference

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

If you intended to correct an error in the post then please contact us.

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?

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!