473,406 Members | 2,710 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.

calling the base destructor.

I create 2 ManagedC++ class inherithing from each other.
I can't see how do I call super destructor or ensure it's called :-/
template <class T>
public ref class CArray : System::IDisposable
{
protected:
~CArray()
{
if( ptr ) // I'm not sure of what IDisposable will do...
free( ptr );
ptr = nullptr;
}
private:
int length;
T* ptr;
};
template <class T>
public ref class CVector : CArray<T>
{
protected:
~CVector()
{
::~CArray();
}
};

Nov 17 '05 #1
7 2344
You don't. C++ is not Pascal. The base destructor is called
automatically, always in the opposite order of the construction. In
other words, C++/CLI destructors work like C++ destructors from this aspect.

You don't have to (= shouldn't) inherit from IDisposable. It's
automatically done when a ref class has a destructor.

Sample code:

using namespace System;

ref class B
{
public:
~B() { Console::WriteLine("~B"); }
};

ref class D : public B
{
public:
~D() { Console::WriteLine("~D"); }
};

int main(array<System::String ^> ^args)
{
{ D d; }
Console::ReadKey(true);
return 0;
}

This outputs
~D
~B

as it would in native C++.

Note that if you allocate the class with gcnew, you must delete it, just
like in native C++:

D* d = gcnew D;
[...]
delete d;

You may want to use a finalizer in order to ensure that the native
resources are destroyed even if the caller forgets about delete. Also
note that in other languages programmers call the Dispose() method
instead of the delete operator, such as d.Dispose() in C#.

Tom
Lloyd Dupont wrote:
I create 2 ManagedC++ class inherithing from each other.
I can't see how do I call super destructor or ensure it's called :-/
template <class T>
public ref class CArray : System::IDisposable
{
protected:
~CArray()
{
if( ptr ) // I'm not sure of what IDisposable will do...
free( ptr );
ptr = nullptr;
}
private:
int length;
T* ptr;
};
template <class T>
public ref class CVector : CArray<T>
{
protected:
~CVector()
{
::~CArray();
}
};

Nov 17 '05 #2
If you implement IDisposable, the destructor of your base class is
automatically chained to in C++. Some possibly helpful posts from my blog:

http://blogs.msdn.com/arich/archive/...23/233683.aspx
http://blogs.msdn.com/arich/archive/...09/427389.aspx

-ATR-

"Lloyd Dupont" wrote:
I create 2 ManagedC++ class inherithing from each other.
I can't see how do I call super destructor or ensure it's called :-/
template <class T>
public ref class CArray : System::IDisposable
{
protected:
~CArray()
{
if( ptr ) // I'm not sure of what IDisposable will do...
free( ptr );
ptr = nullptr;
}
private:
int length;
T* ptr;
};
template <class T>
public ref class CVector : CArray<T>
{
protected:
~CVector()
{
::~CArray();
}
};

Nov 17 '05 #3
> Note that if you allocate the class with gcnew, you must delete it, just
like in native C++:

D* d = gcnew D;
[...]
delete d;

ghh... what's the point?
I mean I write
D^ d = gcnew D();

(and not D* = gcnew D(), beside I didn't know you could create D* with
gcnew, I have to test)
and I expect it to be collected, no?
all this work for naught otherwise!
Nov 17 '05 #4
> D* d = gcnew D;
[...]
delete d;

tested.
apparently it's illegal to write
D* d with managed type.
simple as that!

however, although it's legal to write
D^ d;
delete d;
but I would find it quite.... awful? bad design? badly choosed naming?
to HAVE TO delete MANAGED reference.
however I understand it might be as usefull (or equivalent to?) Dispose()?

And also.. yeah that's right!
Even though I removed inherit from IDisposable I could call Dispose on my
object!! nice ^_^,
I could even used it as an instance of IDisposable^!
Nov 17 '05 #5
thanks, BTW ;-)

"Andy Rich" <Andy Ri**@discussions.microsoft.com> wrote in message
news:80**********************************@microsof t.com...
If you implement IDisposable, the destructor of your base class is
automatically chained to in C++. Some possibly helpful posts from my
blog:

http://blogs.msdn.com/arich/archive/...23/233683.aspx
http://blogs.msdn.com/arich/archive/...09/427389.aspx

-ATR-

"Lloyd Dupont" wrote:
I create 2 ManagedC++ class inherithing from each other.
I can't see how do I call super destructor or ensure it's called :-/
template <class T>
public ref class CArray : System::IDisposable
{
protected:
~CArray()
{
if( ptr ) // I'm not sure of what IDisposable will do...
free( ptr );
ptr = nullptr;
}
private:
int length;
T* ptr;
};
template <class T>
public ref class CVector : CArray<T>
{
protected:
~CVector()
{
::~CArray();
}
};

Nov 17 '05 #6
Lloyd Dupont wrote:
D* d = gcnew D;
[...]
delete d; tested.
apparently it's illegal to write
D* d with managed type.
simple as that!

however, although it's legal to write
D^ d;
delete d;
but I would find it quite.... awful? bad design? badly choosed naming?
to HAVE TO delete MANAGED reference.
however I understand it might be as usefull (or equivalent to?)
Dispose()?


You must call delete IF (and only if) you require deterministic destruction
of your object (e.g. if you're holding native resources). If your object
doesn't have any deterministic destruction requirement, you can just abandon
it and it will be collected.

And also.. yeah that's right!
Even though I removed inherit from IDisposable I could call Dispose
on my object!! nice ^_^,
I could even used it as an instance of IDisposable^!


Yep. That's by design.

-cd
Nov 17 '05 #7
Lloyd Dupont wrote:
D* d = gcnew D;
[...]
delete d;


ghh... what's the point?
I mean I write
D^ d = gcnew D();


Sorry, my bad, that's what I meant. Of course it's D^.

Tom
Nov 17 '05 #8

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

Similar topics

7
by: qazmlp | last post by:
When a member function is declared as virtual in the base class, the derived class versions of it are always treated as virtual. I am just wondering, why the same concept was not used for the...
2
by: Dave | last post by:
Hi, Does the destructor of a derived class will invoke base class destructor implicitly? D
20
by: frs | last post by:
For memory economization, I need to get rid if the virtual destructor. Under this constraint I get caught up in a design, where I need to call a destructor of a derived class from a base class....
6
by: Justin | last post by:
Hello, first time posting. If I have a base class and a derived class, is there only one way to call the base constructor? i.e. Is this the only way I can call the base constructor...
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. ...
3
by: marcwentink | last post by:
Say I have a class A, and a class B that inherits from A. Now A (and B) has a virtual destructor and a virtual function F(); If I now make these statements A* ptrA = new B; ptrA->F(); delete...
3
by: Raider | last post by:
I have library with some class having no virtual functions and non virtual destructor. I want to add new member functions to this class by creating derived class and then use derived class...
6
by: Henrik Goldman | last post by:
Hi I've had a problem with gcc mac osx which I think I figured out. I would like to double check with people here to see if my understanding is correct: I have a class A which class B inherit...
1
by: Rune Allnor | last post by:
Hi all. I am sure this is an oldie, but I can't find a useful suggestion on how to solve it. I have a class hierarchy of classes derived from a base class. I would like to set up a vector of...
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.