How make a smart reference expose methods of basetype ?
-
I have written a reference class for one of my classes. It implements reference counting for that class and that's pretty much it. I have written a cast operator to the class type it references, but the compiler does not use that operator implicitly, so whenever i want to access the referenced object, i have to do an explicit cast (or call a method like getObject()). How can i prevent that ? Here some code to illustrate my problem :
class MyClassReference;
class MyClass
{
public:
void doStuff(); // do that thing you do
static MyClassReference getInstance(); // get an instance of the class, but wrapped in
// my custom reference-class~MyClass();
private:
friend class MyClassReference();MyClass(); // private, so you have to use getInstance(); void addReference(); void removeRererence(); // if (m\_referenceCounter == 0) delete this; int m\_referenceCounter;
};
class MyClassReference
{
public:
MyClassReference(MyClass &object); // call addReference
// fancy copy-constructors and assignment-operators here
operator MyClass &(); // cast to contained object, not used implicitly~MyClassReference(); // call removeReference
};
This is just the principle, feel free to comment on it. wbr Brainley
-
I have written a reference class for one of my classes. It implements reference counting for that class and that's pretty much it. I have written a cast operator to the class type it references, but the compiler does not use that operator implicitly, so whenever i want to access the referenced object, i have to do an explicit cast (or call a method like getObject()). How can i prevent that ? Here some code to illustrate my problem :
class MyClassReference;
class MyClass
{
public:
void doStuff(); // do that thing you do
static MyClassReference getInstance(); // get an instance of the class, but wrapped in
// my custom reference-class~MyClass();
private:
friend class MyClassReference();MyClass(); // private, so you have to use getInstance(); void addReference(); void removeRererence(); // if (m\_referenceCounter == 0) delete this; int m\_referenceCounter;
};
class MyClassReference
{
public:
MyClassReference(MyClass &object); // call addReference
// fancy copy-constructors and assignment-operators here
operator MyClass &(); // cast to contained object, not used implicitly~MyClassReference(); // call removeReference
};
This is just the principle, feel free to comment on it. wbr Brainley
Your operator needs to return the reference to your class:
MyClass& operator& ();
Cédric Moonen Software developer
Charting control [v1.1] -
Your operator needs to return the reference to your class:
MyClass& operator& ();
Cédric Moonen Software developer
Charting control [v1.1]same error : doStuff() is not a member of MyClassReference. -- modified at 6:00 Wednesday 21st February, 2007
-
same error : doStuff() is not a member of MyClassReference. -- modified at 6:00 Wednesday 21st February, 2007
-
MyClassReference ref = MyClass::getInstance();
ref.doStuff(); // C2039: 'doStuff' is not a member of 'MyClassReference'
He does not apply the cast implicitly. The following works :
((MyClass&)ref).doStuff();
The compiler told me though, that i have to declare the cast operator
operator class CNeighbour &();
in order for this to work. But he still does not apply the cast-operator implicitly.
-
MyClassReference ref = MyClass::getInstance();
ref.doStuff(); // C2039: 'doStuff' is not a member of 'MyClassReference'
He does not apply the cast implicitly. The following works :
((MyClass&)ref).doStuff();
The compiler told me though, that i have to declare the cast operator
operator class CNeighbour &();
in order for this to work. But he still does not apply the cast-operator implicitly.
Of course, you are not using the operator at all ! So it is logical that it complains. BTW, are you trying to do something similar as a smart pointer (but using references instead) ?
Cédric Moonen Software developer
Charting control [v1.1] -
MyClassReference ref = MyClass::getInstance();
ref.doStuff(); // C2039: 'doStuff' is not a member of 'MyClassReference'
He does not apply the cast implicitly. The following works :
((MyClass&)ref).doStuff();
The compiler told me though, that i have to declare the cast operator
operator class CNeighbour &();
in order for this to work. But he still does not apply the cast-operator implicitly.
Mr.Brainley wrote:
The compiler told me though, that i have to declare the cast operator
You need to apply that cast. Overloading operation
MyClass
doesn't mean that you can call members ofMyClass
usingMyClassReference
object. By doing this,you can castMyClassReference
class object forMyClass
class.Prasad Notifier using ATL | Operator new[],delete[][^]
-
Of course, you are not using the operator at all ! So it is logical that it complains. BTW, are you trying to do something similar as a smart pointer (but using references instead) ?
Cédric Moonen Software developer
Charting control [v1.1]1. Yes, it's not used. Thought it might be possible to use it implicitly, so the code looks better. 2. Yes. It actually is a smart pointer internally, but i tried to give it reference semantics and syntax. I need this for a synchronization problem. I have a list of references that is accessed from many threads. If one thread retrieves a reference from that list, and the list is cleared while it works on the reference, the reference remains valid until it goes out of scope (if it is a smart reference). This way i can minimize synchronization to the pure access on the list. Maybe it would be better if i just overloaded the '->'-operator. It would have pointer-syntax then, but that would be ok i guess.