By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Subject: Use of new and delete in C++

P: n/a
Hi,

I am working on a way to create a "window" within a dos console. I'm
using Microsoft Visual C++. My main problem is that I am using new to
create a new CHAR_INFO array within the constructor, and I want to destroy
it within the destructor using delete. However, the array is not known
outside of the constructor. This is a class within a class. Until the
object is created, it has no way to know the size of the array; the size is
passed as a parameter to the constructor. Is there a way to make the
destructor aware of the array, or to make it public to the class from within
the constructor? I am using it to store the original information of the
screen that will be overwritten, so that when the window is finished, it can
rewrite the original information (including the background and foreground
color) before the "window" is completely destroyed.

On a side note, has anybody else written advanced console applications? How
did you handle "windows"? If I can get this to work properly, I plan to,
eventually, use it to create menus and so on within the console.

Thanks in Advance,
Eric
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Eric A. Johnson wrote:
I am working on a way to create a "window" within a dos console. I'm
using Microsoft Visual C++. My main problem is that I am using
new to create a new CHAR_INFO array within the constructor, and I
want to destroy it within the destructor using delete. However, the
array is not known outside of the constructor. This is a class
within a class. Until the object is created, it has no way to know
the size of the array; the size is passed as a parameter to the
constructor. Is there a way to make the destructor aware of the
array, or to make it public to the class from within the constructor?
Make your array (pointer) a member. Initialise it in the constructor,
delete it in the destructor. It seems so straightforward that I am
thinking there is a catch in your explanation, which I've missed.

What book are you reading to understand dynamic memory better?
I am using it to store the original information of the screen that
will be overwritten, so that when the window is finished, it can
rewrite the original information (including the background and
foreground color) before the "window" is completely destroyed.
On a side note, has anybody else written advanced console
applications? How did you handle "windows"? If I can get this to
work properly, I plan to, eventually, use it to create menus and so
on within the console.


It doesn't really seem relevant where the dynamic memory is used or
what it represents.

V
Jul 23 '05 #2

P: n/a

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:co********************@comcast.com...
Eric A. Johnson wrote:
I am working on a way to create a "window" within a dos console. I'm
using Microsoft Visual C++. My main problem is that I am using
new to create a new CHAR_INFO array within the constructor, and I
want to destroy it within the destructor using delete. However, the
array is not known outside of the constructor. This is a class
within a class. Until the object is created, it has no way to know
the size of the array; the size is passed as a parameter to the
constructor. Is there a way to make the destructor aware of the
array, or to make it public to the class from within the constructor?
Make your array (pointer) a member. Initialise it in the constructor,
delete it in the destructor. It seems so straightforward that I am
thinking there is a catch in your explanation, which I've missed.

You're right... it was straightforward... I'm just trying to learn so much
at one time, that I guess I overlooked that. Thanks for the tip. What book are you reading to understand dynamic memory better? "C++ From the Ground Up", by Herbert Schildt... is he any good? I have two
C++ books by him. If he isn't good, can you recommend a really good book /
author?
I am using it to store the original information of the screen that
will be overwritten, so that when the window is finished, it can
rewrite the original information (including the background and
foreground color) before the "window" is completely destroyed.
On a side note, has anybody else written advanced console
applications? How did you handle "windows"? If I can get this to
work properly, I plan to, eventually, use it to create menus and so
on within the console.


It doesn't really seem relevant where the dynamic memory is used or
what it represents.

V

Jul 23 '05 #3

P: n/a
Eric A. Johnson wrote:
You're right... it was straightforward... I'm just trying to learn so much
at one time, that I guess I overlooked that. Thanks for the tip.
What book are you reading to understand dynamic memory better? "C++ From the Ground Up", by Herbert Schildt... is he any good? I have

two C++ books by him. If he isn't good, can you recommend a really good book / author?


Please hit:

http://groups-beta.google.com/groups...p.lang.c%2B%2B

This group tends to dislike Schildt. He's a very good writer for warm
friendly prose, and very bad for technical details or thoroughness.

Go ahead and learn from the book, but make sure you read many more. These
can't hurt:

Accelerated C++
The C++ Programming Language, 3rd Edition

Others will recommend more.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 23 '05 #4

P: n/a

"Eric A. Johnson" <no*****@dontlookforme.com> skrev i en meddelelse
news:4o******************@newssvr14.news.prodigy.c om...
Hi,

I am working on a way to create a "window" within a dos console. I'm
using Microsoft Visual C++. My main problem is that I am using new to
create a new CHAR_INFO array within the constructor, and I want to destroy
it within the destructor using delete.
You should use a std::vector (or perhaps a std::string) for this purpose.
However, the array is not known outside of the constructor. This is a
class within a class. Until the object is created, it has no way to know
the size of the array; the size is passed as a parameter to the
constructor. Is there a way to make the destructor aware of the array, or
to make it public to the class from within the constructor? I am using it
to store the original information of the screen that will be overwritten,
so that when the window is finished, it can rewrite the original
information (including the background and foreground color) before the
"window" is completely destroyed.
As Victor Bazarov suggested, it is possible, but beware that you also must
create a correct copy-constructor now. It is much easier just to use the
std:: containers mentioned above.

Peter
[snip]
Thanks in Advance,
Eric

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.