GIDForums  

Go Back   GIDForums > Computer Programming Forums > 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 04-Nov-2007, 00:17
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 94
Algar will become famous soon enough

Parent* parameter, not using child function (virtual in parent)


I am not sure if I am creating a parent class or a child class with this code, but either way, the parent function which is virtual is being called rather then the child function that I want:

Heres various relevant code snippets:

CPP / C++ / C Code:
//The MapCell is a container class which has (among other things) a pointer to a TerBase (parent) with the following function:
//....
void MapCell::setTerrain(TerBase* newTerrain) 
{
	delete terrain;
	terrain = newTerrain;
	terrain->SetRect(left,top,right,bottom);
}
//...

//TerBase paint() declaration:
virtual void paint(CPaintDC* dc);

//Then I have a child of terbase...
class TerFlag : public TerBase 
//...
void paint(CPaintDC* dc);
//...

//Then I have my data manager class which has a 2d vector of MapCells with the following function:
void MapCtrl::setTerAt(const CPoint& index, const TerBase & terrain) 
{
	map[index.x][index.y].setTerrain(new TerBase(terrain));
	paintMap();
}
Hopefully thats just about covers everything. Heres my issue
When from my window event I call the function like this...
CPP / C++ / C Code:
pMap->setTerAt(pMap->pointToIndex(point), TerFlag());

I end up with either a TerFlag object using the TerBase paint() function or a TerFlag object. Either way what I want is a TerFlag object with the TerFlag paint() function !

Can anyone explain to me please what I should change ? I am assuming that its the "new TerBase(terrain)" line thats causeing the problems, but if I don't know in advance which child class is being passed, how can I get around this ?
Last edited by Algar : 04-Nov-2007 at 00:21. Reason: Better formatting
  #2  
Old 04-Nov-2007, 01:11
Algar Algar is offline
Junior Member
 
Join Date: Sep 2007
Posts: 94
Algar will become famous soon enough

Re: Parent* parameter, not using child function (virtual in parent)


Yet another question I solved soon after posting...

I changed the intermedite function to receive a TerBase* instead of a TerBase object.
 
 

Recent GIDBlogOnce again, no time for hobbies 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
How a child process past a value to parent process? Reny Computer Software Forum - Linux 11 09-Apr-2007 09:46
How to return value from child process to parent process Reny C Programming Language 1 16-Mar-2007 11:38
[Tutorial] Function Pointers aaroncohn C++ Forum 4 17-Feb-2006 12:33
[TUTORIAL] Calling an external program in C (Linux) dsmith C Programming Language 4 22-Apr-2005 14:30
Revising Script style ?????? pepee MySQL / PHP Forum 4 14-Apr-2004 05:59

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

All times are GMT -6. The time now is 16:34.


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