Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
OK chaps, assume I have something like this:

typedef std::vector<cmyclassone*> tClassOneVect;

class CMyClassTwo
{
   tClassOneVect m_MyData;
public:
   operator const tClassOneVect() const { return m_MyData; }
   operator tClassOneVect() { return m_MyData; }
}
I've overloaded the cast operator so I can cast an instance of CMyClassTwo into tClassOneVect. OK, with me so far?

So my question is this: Should I declare the cast operator functions like this:

operator tClassOneVect() { return m_MyData; }

or like this:

operator tClassOneVect&() { return m_MyData; }

Note the reference (&) here.

What are your thoughts chaps?
Jim
Posted
Updated 24-Nov-10 3:28am
v3
Comments
Alain Rist 24-Nov-10 9:28am    
Code formatting

1 solution

Hi, Jim,

The two operators have different return types.

- operator tClassOneVect() { return m_MyData; } constructs a temporary tClassOneVect from m_MyData (using tClassOneVect copy constructor) and returns it: caller can do anything on the returned copy, it will not change the original.

- operator tClassOneVect&() { return m_MyData; } returns a reference to this instance of CMyClassTwo::tClassOneVect. Caller can modify it, and it will persist as long as this instance of CMyClassTwo.

- There is a third possibility: operator const tClassOneVect&() { return m_MyData; } which will return a const (read-only) reference to this instance of CMyClassTwo::tClassOneVect. Caller can use it but not modify it.

cheers,
AR
 
Share this answer
 
v2
Comments
Jim @ JCT 24-Nov-10 10:01am    
Thanks for your answer.
So this means that:

CMyClassTwo MyInstance;
tClassOneVect data = (tClassOneVect)MyInstance; //a temporary tClassOneVect gets created and then assigned
tClassOneVect& data = (tClassOneVect&)MyInstance; //Straight reference, but easy to forget the &
tClassOneVect& data = (tClassOneVect)MyInstance; //Uh-oh, reference to temporary

So the first one is OK but inefficient, the second is OK but has to have an awkward & symbol and the third is wrong. Is this correct?
Jim
Alain Rist 24-Nov-10 10:17am    
I am not sure the third compiles without error or at least WARNING.
const tClassOneVect& data = MyInstance; should work and call my answer's second construct (if exists) :)
Jim @ JCT 24-Nov-10 10:40am    
The third one *does* appear to compile without warning. Ouch.

"const tClassOneVect& data = MyInstance; should work..." And that's the beauty of the cast operator. But I usually have to explicitly cast it, becuase the main reason for doing the above in the first place is to do this:
foreach(cmyclassone* pMyClassOneInstance, (tClassOneVect&)MyInstance)
//do something with pMyClassOneInstance

So the reference is a necessary 'wart' on my code.
Thanks Alain!
Jim

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900