473,788 Members | 2,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Destructor and print visitor

Hi

I have a problem where i have allocated an array on the heap, using
new in a class called "Array". In the end of my main i call a visit
method that visits all of the elements in class "Array" using a
visitor object

eg.

// visits each element in the list
template <class T>
void Array<T>::visit (Visitor<T> aVisitor)
{
// create an iterator object
ArrayIterator<T > iter(*this);

// start visit
aVisitor.startV isit();

// do visit
for(iter.begin( ); !iter.atEnd(); ++iter)
aVisitor.doVisi t(*iter);

// end visit
aVisitor.endVis it();
}

I have a visitor that prints each element using cout. If i use this
visitor it will print garbage if i have a destructor in the Array class.
If i delete the destructor in the Array class then it all works fine
(except that i do not free my memory). I figure that i need to delay the
call of the destructor in the Array class or i need to ensure that my
program do not continue while my visitor prints each elements using cout.
How do i ensure that my data in the array class is not deleted from the
heap before i have used it.

Thanks.
Jul 22 '05 #1
2 1504
Lars Tackmann wrote:

How do i ensure that my data in the array class is not deleted from the
heap before i have used it.


By adjusting the scopes such that this will not happen.
Please post a short, complete, compilable program which demonstrates
your problem.
Your problem description sounds as if there is more to the *real* problem
then you have already recognized.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #2
"Lars Tackmann" <ro****@diku.dk > wrote...
I have attaced [...]


http://www.parashift.com/c++-faq-lit...t.html#faq-5.4
http://www.parashift.com/c++-faq-lit...t.html#faq-5.8
Jul 22 '05 #3

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

Similar topics

3
2733
by: hycn office | last post by:
how to print with pre-define preference such as margin and page size ? and a quick Question , the function call to save as thanks ~~~~~~~
9
1716
by: djskrill | last post by:
Why is the Constructor called 4 times but the Destructor 5 times? I am using MS VC 6. If this has been covered already please let me know. The Code: #include <stdio.h> class MyClass { private: int x;
12
1819
by: ypjofficial | last post by:
Hello all, I have encountered with following strange problem. I am coding in C++ and using VC++ 6 compiler. I have a class strvector containing char * cstr as a private member and i have defined its destructor for releasing memory hold by cstr. In main i created a vector<strvector> and whenever i put a object of strvector inside this vector the program crashes.
2
2183
by: Ben Amada | last post by:
Hi group. I'm going to display a low resolution image in an HTML page. On the web server, I have a high resolution version of that image. If I display the high resolution image in the browser for the visitor to print, won't the DPI of the image be reduced to 72 dpi and lose resolution when the visitor goes to print the image? If this is wrong, please let me know. If this is correct, is there a way in JavaScript to be able to send the...
4
3810
by: Joe | last post by:
I am looking for the quintessential blueprint for how a C++ like destructor should be implemented in C#. I see all kinds of articles in print and on the web, but I see lots of discrepencies. For example ... C# Essentials (O'Reilly) - "Finalizers are class-only methods". Not sure what "class-only method" means in this context, but ususally I would assume that to mean it is static and would only be called once for the life of the...
12
3040
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of itself. The called class has special functions for each class that can call it. With the visitor pattern, the calling class can have new operations added without being changed itself.
1
1955
TMS
by: TMS | last post by:
I'm a little rusty with C++ so bear with me. My assignment has me amending a database file by adding 'tombstones' to records that are to be omitted later by the destructor. The database brings in a file (lets call it A) then there is a menu of operations. If the user wants to remove a file, they use the remove option which places a 'b' (in this case) in front of the Social Security Number. That is all fine and good. My amendments take...
5
11362
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget to pass all the necessary object files to the linker. Neither of those are my problem. Please bear with me as the question I ask is rather long and I think it's beyond a CS101 level of linker stupidity. If it is a stupid CS101 mistake I'm making...
5
1701
by: Marcin201 | last post by:
I have a class which uses a temporary directory for storing data. I would like that directory to be removed when the class is no longer used. I have tried removing the temporary directory from the class destructor, however, it was never called. After I while I traced the problem to the class having a reference to it's own function. Here is a simplified model. test.py class Foo: def __init__(self):
0
9498
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
10112
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
9969
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...
1
7518
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
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5399
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
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.