473,799 Members | 3,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::IDispos able
{
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 2364
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::WriteL ine("~B"); }
};

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

int main(array<Syst em::String ^> ^args)
{
{ D d; }
Console::ReadKe y(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::IDispos able
{
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::IDispos able
{
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**@discussion s.microsoft.com> wrote in message
news:80******** *************** ***********@mic rosoft.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::IDispos able
{
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
1891
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 destructors. What I am expecting is, if the destructor is declared as virtual in base, the destructors of all its derived classes also should be virtual always. What exactly is the reason for not having it so?
2
1817
by: Dave | last post by:
Hi, Does the destructor of a derived class will invoke base class destructor implicitly? D
20
2755
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. Briefly it could be displayed like the code below. It ends up deleting the member 'x' of 'B' twice. Why? Is there a way around? Thanks Frank
6
2109
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 (presuming the base constructor took an int): Derived::Derived(int a) : Base(a)
3
2238
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. Hoe do I call it? private void DefineFinalizer(TypeBuilder typeBuilder) {
3
1917
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 ptrA then in the statement ptrA->F(), by means of the polymorph behavior,
3
3151
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 instances as a base class instances: // -------- library -------- class B { ...
6
5644
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 from. A has a pure virtual function virtual in f1() = 0. In B I implement this function and the compiler works out the code. However from A's destructor B can be compiled and then the runtime aborts with saying that a call to a pure virtual...
1
2529
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 smart pointers to the base class, and access the virtual functions of any class in the hierarchy through the virtual functions.
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10231
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9073
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.