Having Trouble Calling ! Finalizer because of Operator Overloads

H

Howard Swope

Greetings:

C++ CLR .Net 2

I have a ref class that I have created that wraps an unmanaged pointer. It
acts like a smart pointer for reference counted objects for a particular
library I am working in. I have overloaded the ->, *, assignment, and
equality operators. I have created a ! finalizer and call it from my
destructor. However, when I do so, the operator overload returns the type of
the wrapped pointer.

~MyClass()
{
this->!MyClass();
}

I get an error saying "WrappedPointer type doesn't have a function
!MyClass." I am having a hell of a time trying to call this. I am trying a
bunch of different things. Either it won't compile or I end in an endless
loop in the destructor. I know there is a way to do this.

???

Thanks
 
H

Howard Swope

OK figured it out... simple... just added a little indirection:

public ref class FooMPtr

{

private:

Foo* niObj;

void Disp()

{

if (niObj != nullptr)

niObj->DecRefCount();

}

!FooMPtr()

{

Disp();

}

internal:

FooMPtr()

{

niObj = nullptr;

}

FooMPtr(Foo* ptr)

{

niObj = ptr;

if (niObj != nullptr)

niObj->IncRefCount();

}

FooMPtr(FooMPtr^% mptr)

{

niObj = mptr;

if (niObj != nullptr)

niObj->IncRefCount();

}

virtual ~FooMPtr()

{

Disp();

}

operator Foo*()

{

return niObj;

}

Foo& operator *()

{

return *niObj;

}

Foo* operator ->()

{

return niObj;

}

FooMPtr^ operator=(FooMPtr^% mptr)

{

if (niObj != mptr)

{

if (niObj != nullptr)

niObj->DecRefCount();

niObj = mptr;

if (niObj != nullptr)

niObj->IncRefCount();

}

return this;

}

FooMPtr^ operator=(Foo* ptr)

{

if (niObj != ptr)

{

if (niObj != nullptr)

niObj->DecRefCount();

niObj = ptr;

if (niObj != nullptr)

niObj->IncRefCount();

}

return this;

}

bool operator == (Foo* ptr)

{

return niObj == ptr;

}

bool operator != (Foo*ptr)

{

return niObj != ptr;

}

bool operator == (FooMPtr^% mptr)

{

return niObj == mptr;

}

bool operator != (FooMPtr^% mptr)

{

return niObj != mptr;

}

};
 
B

Ben Voigt [C++ MVP]

Howard Swope said:
Greetings:

C++ CLR .Net 2

I have a ref class that I have created that wraps an unmanaged pointer. It
acts like a smart pointer for reference counted objects for a particular
library I am working in. I have overloaded the ->, *, assignment, and
equality operators. I have created a ! finalizer and call it from my
destructor. However, when I do so, the operator overload returns the type
of the wrapped pointer.

~MyClass()
{
this->!MyClass();
}

Try instead:

MyClass::!MyClass();

Or just move the code to an inline helper function called from both
finalizer and destructor.
I get an error saying "WrappedPointer type doesn't have a function
!MyClass." I am having a hell of a time trying to call this. I am trying a
bunch of different things. Either it won't compile or I end in an endless
loop in the destructor. I know there is a way to do this.

Yeah, you tried !MyClass(), which constructs a new MyClass and then uses the
logical negation operator, then destroys the temporary. The fully qualified
syntax I gave above should work.
???

Thanks
--
Howard Swope [hswope.swopeATnavteqDOTcom]
Senior Software Developer
Media Development
Navteq Traffic [http://www.traffic.com]
 
H

Howard Swope

That's got it...

Thanks Ben

Ben Voigt said:
Howard Swope said:
Greetings:

C++ CLR .Net 2

I have a ref class that I have created that wraps an unmanaged pointer.
It acts like a smart pointer for reference counted objects for a
particular library I am working in. I have overloaded the ->, *,
assignment, and equality operators. I have created a ! finalizer and call
it from my destructor. However, when I do so, the operator overload
returns the type of the wrapped pointer.

~MyClass()
{
this->!MyClass();
}

Try instead:

MyClass::!MyClass();

Or just move the code to an inline helper function called from both
finalizer and destructor.
I get an error saying "WrappedPointer type doesn't have a function
!MyClass." I am having a hell of a time trying to call this. I am trying
a bunch of different things. Either it won't compile or I end in an
endless loop in the destructor. I know there is a way to do this.

Yeah, you tried !MyClass(), which constructs a new MyClass and then uses
the logical negation operator, then destroys the temporary. The fully
qualified syntax I gave above should work.
???

Thanks
--
Howard Swope [hswope.swopeATnavteqDOTcom]
Senior Software Developer
Media Development
Navteq Traffic [http://www.traffic.com]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top