GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-Nov-2007, 16:19
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 92
Algar will become famous soon enough

Constructors and scope issue


Ok, well theres a lot of code to this problem so ill describe as best I can instead of showing 10 pages.

I have a container class MapCell
Inside that class there are pointers to 2 other classes CharBase and TerBase. All 3 classes are derived from CRect (doubt thats relevant)

I am trying to manage memory completely from within the container class.
Heres the members from MapCell that might be relevant to the problem:
(It might be better to skip this block of code and look at the next before coming back)

CPP / C++ / C Code:
TerBase* terrain;		//pointer to dynamic layer 1
CharBase* character;	//pointer to dynamic layer 2

MapCell::MapCell() { //default constructor
	terrain = new TerBase();
	character = new CharBase();
}

MapCell::MapCell(CPoint topLeft, CPoint botRight) { //Rect constructor
	terrain = new TerBase();
	character = new CharBase();
	setSizePos(topLeft, botRight);
}

MapCell::MapCell(const MapCell & mapCell) { //copy constructor
	terrain = new TerBase(mapCell.getTerrain());
	character = new CharBase(mapCell.getCharacter());
}

MapCell::~MapCell() { //destructor
	delete character;
	delete terrain;
}

CharBase MapCell::getCharacter() const {	//deep copy		
	return CharBase(*character);
}

TerBase MapCell::getTerrain() const { 	//deep copy
	return TerBase(*terrain);
}

So thats generally how the copying / construction / destruction is handled

but now when I go to populate a 2d vector of these MapCells I have some big problems.
CPP / C++ / C Code:
	const int SIZE = 30;
	map.resize(10);
	for (UINT i=0; i<map.size(); i++)
		map[i].resize(10);

	for (UINT i=0; i<map.size(); i++) {
		for (UINT j=0; j<map[i].size(); j++) {
			MapCell cell;
			CPoint tl(i*SIZE, j*SIZE);
			CPoint br(tl.x+SIZE, tl.y+SIZE);
			cell.setSizePos(tl,br);
			map[i][j] = cell;
		}
	}
Its probably emmediately apparent, but basically the cell is created inside the block, then the destructor is called after basically deleting both pointers.

I am not sure if I am just not populating the vector properly, or my construction / copy / destruction is wrong.

Any help appreciated, thanks.
  #2  
Old 02-Nov-2007, 16:57
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 92
Algar will become famous soon enough

Re: constructors and scope issue


Ok so I changed the way I am populating the vector.

CPP / C++ / C Code:
	const int SIZE = 30;
	vector<MapCell> x(10, MapCell());
	for (int i=0; i<10; i++)
		map.push_back(x);

	for (UINT i=0; i<10; i++) {
		for (UINT j=0; j<10; j++) {
			map[i][j].setSizePos(CPoint(i*SIZE,j*SIZE), CPoint((i+1)*SIZE,(j+1)*SIZE));
		}
	}

And now it appears to be working fine.

Is it that pushback makes a deep copy , but = is shallow ?
  #3  
Old 03-Nov-2007, 01:29
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: constructors and scope issue


Quote:
Originally Posted by Algar
Is it that pushback makes a deep copy , but = is shallow ?
They both do whatever the class copy constructor and assignment operator do. Since you don't have an overloaded assignment operator, a member-wise copy is what you get.

I didn't look at your code, though, but if you tested it's working now, then the assignment operator is most likely the cause.
__________________
Music, programming, endless learning..
  #4  
Old 03-Nov-2007, 01:52
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 92
Algar will become famous soon enough

Re: constructors and scope issue


Ok, thanks for the info !
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing constructors of a base class explicitly in the derived class nethravathi.mn CPP / C++ Forum 1 27-Sep-2006 17:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:41.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.