|
-
Dec 14th, 2002, 12:15 PM
#1
Thread Starter
Lively Member
deleting class array
Hi, I've been able to create an array of class variables, but I can't get the program to delete them without an access violation.
Thanks!
PHP Code:
class TEST
{
public:
TEST(int y);
int x;
};
TEST::TEST(int y)
{
x=y;
}
class TEST2
{
public:
TEST *test[];
};
void main()
{
TEST2 t2;
for(int i=0; i<6;i++)
t2.test[i] = new(TEST)(i); // creating
// delete them here
}
-
Dec 14th, 2002, 12:32 PM
#2
Member
What exactly are you tring to do?
I guess you would just do something like this:
Code:
for(i = 0; i < 6; i++)
delete t2.test[i];
Normally class members are destroyed in the classes Destructor... It is automatically called by 'delete'
Code:
class TEST
{
public:
TEST(void);
// This is the destructor.
~TEST(void);
};
TEST::~TEST(void)
{
for(int i = 0; i < 6; i++)
delete this->test[i];
}
-
Dec 14th, 2002, 01:38 PM
#3
Where the deletion is doesn't matter. And since the array is a member of TEST2 it certainly won't be in the TEST destructor.
But the core deletion code should work.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 14th, 2002, 01:53 PM
#4
Thread Starter
Lively Member
It still is giving an access violation when it tries to delete the pointer.
PHP Code:
TEST2::~TEST2()
{
for(int i=0; i<6;i++)
delete test[i];
}
I've tried how many different sorts of combinations with delete but whatever I try it keeps having an access violation.
-
Dec 14th, 2002, 02:07 PM
#5
Oh, and did I mention that [] is bad to use here anyway? This array has no lenght. You may not access it's elements because it has none. Declare test as
TEST *test[6];
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 14th, 2002, 06:11 PM
#6
Thread Starter
Lively Member
I can't place
TEST *test[6];
because I want it to be a dynamic array, thats whats been giving me problems, I figured you would just start with
TEST *test[]; but trying to delete it won't work, so I don't know how to have the dynamic array and still delete it
-
Dec 14th, 2002, 07:59 PM
#7
Then I suggest you don't use an array of pointers but rather one pointer and use array new:
Code:
TEST *ar;
ar = new TEST[6](1);
// ...
delete[] ar;
Note that you can't pass individual arguments to the constructors anymore, only the same to each constructor. But this syntax is far easier to use. Else you'd have to use:
Code:
TEST **ar;
ar = new TEST*[6];
for(int i=0;i<6;++i)
ar[i] = new TEST(i);
// Access:
ar[3]->DoSomething();
// Deletion:
for(int i=0;i<6;++i)
delete ar[i];
delete[] ar;
Quite bad, and terribly prone to memory leaks.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 14th, 2002, 09:05 PM
#8
Thread Starter
Lively Member
what I've meant is instead of allocating 6 I want that to be a variable that gets increased everytime I call a function of mine. I've been able to get it to work with a fixed amount to begin with, but not when its a variable
-
Dec 14th, 2002, 10:01 PM
#9
transcendental analytic
you can allocate a variable amount of objects with new
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Dec 15th, 2002, 05:03 AM
#10
But if that amount changes you have to reallocate the memory every time. In that case you should rather use a vector:
Code:
#include <vector>
using std::vector;
typedef vector<TEST> testvec;
testvec array;
// add a new element:
array.push_back(TEST(4));
No deletion needed, the vector does it all for you.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 15th, 2002, 09:07 AM
#11
Thread Starter
Lively Member
that works much better thank you! is there any tutorial that shows more about the vector, like how to go about shrinking the vector array and swapping values
-
Dec 15th, 2002, 07:07 PM
#12
I don't know if there is a tutorial for vectors, but a good reference should suffice. Look up vector in MSDN or on SGI's reference page.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|