Jump to content

  • Log In with Google      Sign In   
  • Create Account


Defining a member function that returns a reference


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 GodFear   Members   -  Reputation: 151

Like
0Likes
Like

Posted 16 April 2012 - 09:28 AM

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.

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7869

Like
3Likes
Like

Posted 16 April 2012 - 09:52 AM

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.
class CEntity
{
public:
	CEntity();
	virtual ~CEntity();
	virtual const TVector<float, 3>& GetVector() = 0;
};

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>>.

#3 GodFear   Members   -  Reputation: 151

Like
0Likes
Like

Posted 16 April 2012 - 12:52 PM

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.

class CEntity
{
public:
	CEntity();
	virtual ~CEntity();
	virtual const TVector<float, 3>& GetVector() = 0;
};

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()


#4 Brother Bob   Moderators   -  Reputation: 7869

Like
1Likes
Like

Posted 16 April 2012 - 01:07 PM

Yes, that is correct.

#5 GodFear   Members   -  Reputation: 151

Like
0Likes
Like

Posted 17 April 2012 - 06:34 AM

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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS