473,406 Members | 2,312 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,406 software developers and data experts.

C++ hiding Dispose is BAD

Anyone know a direct translation for this C# code? I know I can make a new
function, of course, but that's clumsy.

assume (o is IDisposable)
MethodInvoker d = o.Dispose;

I tried taking the address of the destructor, that either tries to give me
protected void Dispose(bool) with an error (it's the wrong argument list) or
tells me {dtor} is not a member of IDisposable.

This is all on VS2005 SP1.

Automatic implementation of IDisposable screwed up this one!
Oct 22 '08 #1
8 2126
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:#x*************@TK2MSFTNGP06.phx.gbl...
Anyone know a direct translation for this C# code? I know I can make a
new function, of course, but that's clumsy.

assume (o is IDisposable)
MethodInvoker d = o.Dispose;

I tried taking the address of the destructor, that either tries to give me
protected void Dispose(bool) with an error (it's the wrong argument list)
or tells me {dtor} is not a member of IDisposable.


Does this work?

////////////////////////////////////////////////////////////////////////

ref class disposableclass
{
public:
disposableclass()
{
}
~disposableclass()
{
this->!disposableclass();
}
!disposableclass()
{
}
};

....

disposableclass ^o = gcnew disposableclass();
MethodInvoker ^d = gcnew MethodInvoker(o,
&disposableclass::~disposableclass);

////////////////////////////////////////////////////////////////////////
Mark

--
Mark Salsbery
Microsoft MVP - Visual C++
Oct 23 '08 #2
Mark Salsbery [MVP] wrote:
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:#x*************@TK2MSFTNGP06.phx.gbl...
>Anyone know a direct translation for this C# code? I know I can
make a new function, of course, but that's clumsy.

assume (o is IDisposable)
MethodInvoker d = o.Dispose;

I tried taking the address of the destructor, that either tries to
give me protected void Dispose(bool) with an error (it's the wrong
argument list) or tells me {dtor} is not a member of IDisposable.

Does this work?
Your code compiles cleanly. So my situation is actually less general than I
first thought.

Change disposableclass so that it inherits System::Windows::Forms::Form, for
instance. Then:

..\CppTestJunk.cpp(28) : error C2248: 'disposableclass::Dispose' : cannot
access protected member declared in class 'disposableclass'

..\CppTestJunk.cpp(21) : compiler has generated 'disposableclass::Dispose'
here

..\CppTestJunk.cpp(8) : see declaration of 'disposableclass'

..\CppTestJunk.cpp(28) : error C3352: 'void disposableclass::Dispose(bool)' :
the specified function does not match the delegate type 'void (void)'

>
////////////////////////////////////////////////////////////////////////

ref class disposableclass
{
public:
disposableclass()
{
}
~disposableclass()
{
this->!disposableclass();
}
!disposableclass()
{
}
};

...

disposableclass ^o = gcnew disposableclass();
MethodInvoker ^d = gcnew MethodInvoker(o,
&disposableclass::~disposableclass);

////////////////////////////////////////////////////////////////////////
Mark

Oct 29 '08 #3
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:ew**************@TK2MSFTNGP03.phx.gbl...
>
Your code compiles cleanly. So my situation is actually less general than
I first thought.

Change disposableclass so that it inherits System::Windows::Forms::Form,
for instance.

In that case, Dispose() is hidden (by access level change) by the Form
class and re-exposed through its Close() member, so yeah, that won't work.

Oct 30 '08 #4
"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospamwrote in message
news:#e**************@TK2MSFTNGP06.phx.gbl...
>
In that case, Dispose() is hidden (by access level change) by the Form
class and re-exposed through its Close() member, so yeah, that won't work.


re-exposed is the wrong wording...I meant iDispose()'s functionality is
re-exposed through Close().

The derived class would need to implemented in a similar fashion.

Mark

--
Mark Salsbery
Microsoft MVP - Visual C++
>

Oct 30 '08 #5


"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospamwrote in message
news:#e**************@TK2MSFTNGP06.phx.gbl...
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:ew**************@TK2MSFTNGP03.phx.gbl...
>>
Your code compiles cleanly. So my situation is actually less general
than I first thought.

Change disposableclass so that it inherits System::Windows::Forms::Form,
for instance.


In that case, Dispose() is hidden (by access level change) by the Form
class and re-exposed through its Close() member, so yeah, that won't work.
..NET doesn't allow changing access level at inheritance. But the C++/CLI
compiler is considering the destructor reference to mean protected
Dispose(bool) instead of public Dispose(void).

Oct 30 '08 #6
"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:0B**********************************@microsof t.com...
>
.NET doesn't allow changing access level at inheritance.
Yeah I always believe that in C#.

What magic does C++ use to make this fail to compile:

System::Windows::Forms::Form ^frm = gcnew
System::Windows::Forms::Form();
frm->Dispose();
???

Oct 30 '08 #7
Duh - C++ doesn't let you call Dispose().
///////////////////////////////////////

ref class disposableclass : public System::Windows::Forms::Form
{
public:
disposableclass()
{
}
~disposableclass()
{
this->!disposableclass();
}
!disposableclass()
{
}
};

....
disposableclass ^o = gcnew disposableclass();
o->~disposableclass(); // This is ok
MethodInvoker ^d = gcnew MethodInvoker(o,
&disposableclass::~disposableclass); // This is not ok?

///////////////////////////////////////
Bug?

Oct 30 '08 #8
Not sure what you meant by that last comment.

delete (some_tracking_handle);

does exactly call Dispose on the object if it implements IDisposable.

"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospamwrote in message
news:uH**************@TK2MSFTNGP03.phx.gbl...
Duh - C++ doesn't let you call Dispose().
///////////////////////////////////////

ref class disposableclass : public System::Windows::Forms::Form
{
public:
disposableclass()
{
}
~disposableclass()
{
this->!disposableclass();
}
!disposableclass()
{
}
};

...
disposableclass ^o = gcnew disposableclass();
o->~disposableclass(); // This is ok
MethodInvoker ^d = gcnew MethodInvoker(o,
&disposableclass::~disposableclass); // This is not ok?

///////////////////////////////////////
Bug?
Oct 31 '08 #9

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

Similar topics

2
by: Noloader | last post by:
Hello, Access XP, SQL Server 2000 Is it possible to hide a SP under Queries in Access, yet still be able to Execute it from Access? (Similar to hiding Tables, then using Views) We hooked...
1
by: Alexander | last post by:
I am building a little unique dialog editor and have derived some new classes like DButton, DLabel and DTextBox. The user adds these Controls to a panel which is part of a class Document which is...
17
by: Bob Weiner | last post by:
What is the purpose of hiding intead of overriding a method? I have googled the question but haven't found anything that makes any sense of it. In the code below, the only difference is that...
1
by: Amber | last post by:
The DataGrid allows you to make columns visible or invisible on demand - even edit and other special columns. This article will show you how it is done. Some developers have reported problems...
4
by: Tony Vitonis | last post by:
Hello. I've written an app that I want to "live" in the system tray. I want it to start up with just a tray icon showing, and if the user selects "Settings..." from the icon's context menu, to...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
7
by: Steve954 | last post by:
I recently installed and started using VS2005 on a Windows XP Pro machine. When creating a new windows forms project I find that I am unable to hide any forms using the me.hide() or me.visible =...
11
by: Alex | last post by:
Hello all, I have a main form(say "form1") .i want to display another form(say "form2") on occuring of an event (say a button click) and want to hide it after some time so that it will again...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.