[C++][Solved]Anyone good with pointers and constructors?

Questions about programming languages and debugging
Post Reply
User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

[C++][Solved]Anyone good with pointers and constructors?

Post by ayu »

I think, that if I sit down and really think about this matter, I would be able to solve it, but every time I try it, I get a splitting head ache.

Imagine the following situation:

A class contains a double pointer, that points towards an array of pointers, each and every one of the pointers in the array points towards an object which contains a new double pointer with a new array of pointers that points to a new set of objects.

Now, how would I create a copy-constructor and create an operator overloading for such a construction? ^^

I can work out one double pointer with one array, but at this very moment my brain can't handle one more, so if anyone is good with pointers then a little push in the right direction would be appreciated ^^

Anyway, happy new year!
Last edited by ayu on 28 Jan 2009, 05:01, edited 1 time in total.
"The best place to hide a tree, is in a forest"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Passed midnight coding is healthy =)

issue has been solved, was rather easy when you look at it now, although not tested yet it looks like it will work, will be tested when the application is test-compiled later ^^

Code: Select all

Engine::~Engine()
{
	for(int i = 0; i < mNumCategories; ++i)
	{
		if(mCategory[i] != NULL)
		{
			mCategory[i]->~Category;
			delete mCategory[i];
		}
	}

	delete [] mCategory;
}
"The best place to hide a tree, is in a forest"

ebrizzlez
Kage
Kage
Posts: 732
Joined: 31 Mar 2007, 16:00
17
Location: Hidden in a Buffer Protection.
Contact:

Post by ebrizzlez »

Bravo cats. I was thinking the same thing, but did you allocate the pointers with "new" ?

Ex:

Code: Select all

int *mCategory[] = new int;
[img]http://i81.photobucket.com/albums/j205/ebrizzlez/4lsint1.jpg[/img]

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

ebrizzlez wrote:Bravo cats. I was thinking the same thing, but did you allocate the pointers with "new" ?

Jupp :)
"The best place to hide a tree, is in a forest"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Update:

Didn't work ^^ gives the following error

Code: Select all

1>Engine.cpp
1>.\Engine.cpp(27) : fatal error C1001: An internal error has occurred in the compiler.
1>(compiler file 'msc1.cpp', line 1411)
It would seem as if you can not call a destructor like that. So I will have to make a separate function to do it for me, and I'll update this thread again when I have done so and seen that it works.
"The best place to hide a tree, is in a forest"

User avatar
Swan
Knight of the Sword
Knight of the Sword
Posts: 827
Joined: 18 Oct 2006, 16:00
17
Contact:

Post by Swan »

Yes cats, would be very grateful if you WOULD post your solution. I am very intrigued now =)

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Post by ayu »

Swan wrote:Yes cats, would be very grateful if you WOULD post your solution. I am very intrigued now =)
Sure thing buddy ^^ here is the solution

Code: Select all

Engine::~Engine()
{
	for(int i = 0; i < mNumCategories; ++i)
	{
		if(mCategory[i] != NULL)
		{
			mCategory[i]->EmptyCategory();
			delete mCategory[i];
		}
	}

	delete [] mCategory;
}

Code: Select all

void Category::EmptyCategory()
{
	for(int i = 0; i < mNumItems; ++i)
	{
		if(mItem[i] != NULL)
			delete mItem[i];
	}

	delete [] mItem;
}
"The best place to hide a tree, is in a forest"

Post Reply