473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Having Trouble Calling ! Finalizer because of Operator Overloads

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
--
Howard Swope [hswope.swopeATnavteqDOTcom]
Senior Software Developer
Media Development
Navteq Traffic [http://www.traffic.com]
Jan 10 '08 #1
3 1185
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;

}

};

"Howard Swope" <hswopeATtrafficDOTcomwrote in message
news:O7**************@TK2MSFTNGP02.phx.gbl...
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
--
Howard Swope [hswope.swopeATnavteqDOTcom]
Senior Software Developer
Media Development
Navteq Traffic [http://www.traffic.com]

Jan 10 '08 #2

"Howard Swope" <hswopeATtrafficDOTcomwrote in message
news:O7**************@TK2MSFTNGP02.phx.gbl...
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]

Jan 10 '08 #3
That's got it...

Thanks Ben

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:ep**************@TK2MSFTNGP02.phx.gbl...
>
"Howard Swope" <hswopeATtrafficDOTcomwrote in message
news:O7**************@TK2MSFTNGP02.phx.gbl...
>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]


Jan 10 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: KK | last post by:
Hi, im working on this bigInt class. Need help writing algorithm for the operator*, andy help will be appreciated. Thanks in advance bigInt.h...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
3
by: BravesCharm | last post by:
BravesCharm Dec 7, 10:57 am show options Newsgroups: microsoft.public.dotnet.distributed_apps From: "BravesCharm" <mastrauc...@gmail.com> Date: 7 Dec 2004 10:57:40 -0800 Local: Tues, Dec...
3
by: esafran | last post by:
I've defined a class, but now I want to Define a Finalizer (destructor)... How do I call the Base Finalizer???, void Finalize() is a protected override method and Type.GetType Does not work. ...
6
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a...
5
by: richard.parker | last post by:
Hello, I need to overload operator new with affecting the system libraries. Has anyone done this? I've got 2 static libraries and application source code where the operator needs to be...
7
by: Jeremy Chaney | last post by:
I have an application written in C# that uses objects written in a managed C++ DLL. When I exit my app, my C# classes have their destructors called, but the MC++ objects that those classes hold...
4
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Operator overloads are just like any other member function, you can make them do whatever you want. However, of course, we might expect them to behave in a certain way. The ++ operator should...
8
by: Paul E Collins | last post by:
This code won't compile because "the modifier 'abstract' is not valid for this item". abstract class X { public abstract static bool operator >(X a, X b); public abstract static bool operator...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.