![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
pointer in base class to derived class
So I have this grid and it is made up of cells
class Cell{
private:
int value;
public:
void set_value(int in);
int get_value(void);
int event_handler(void);
// other code
};class vec{
private:
Cell* array[10];
public:
void set_array(Cell* in);
int event_handler(void);
//other code
};
class Grid{
private:
vec[100];
public:
void event_handler(void);
// and other code
};Vec contains and array of pointers to cell so I use vect to controll the behavior of the cell in vec. It is possible, howerver that the address (or controll of a cell) can be in two different vec's. When the value of Cell changes I want that Cell to send a signal to each of its vec so that each vec can deal with the change in Cell. Right now I have a class Grid and the cell's event is returned through vec event_handler and that is returned to Grid event_handler. How can I have a pointer in Cell to point to vec? I have read of using dynamic pointers but I'm not sure on the syntax but I have also read that using a dynamic pointer is a flaw in C++ and shouldn't realy be use. Or is there another way to deal with my situation? Thanks, Milton |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5
![]() |
Look up forward declarations.
In Cell.h class vec; //forward declaration
class Cell
{
public:
void SetParent(vec *the_parent) {parent = the_parent;};
void event_handler();
private:
vec *parent;
};class Cell; //forward declaration
class vec{
private:
Cell* array[10];
public:
void set_array(Cell* in);
int event_handler(void);
//other code
};Now, in the implementation of the Cell class (eg cell.cpp) you need to include both headers, so you can call members from either class; #include "vec.h"
#include "Cell.h"
void Cell::event_handler()
{
parent->event_handler();
}This should be enough to get you started .... |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
Thanks. I'll work with it.
Milt |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|