Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 4th, 2006, 10:30 PM   #1
ionexchange
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 ionexchange is on a distinguished road
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
};
This grid is made up row and columns which are the same type
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
ionexchange is offline   Reply With Quote
Old Jan 4th, 2006, 10:56 PM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,221
Rep Power: 5 grumpy is on a distinguished road
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;
};
In vec.h, you can also do a similar thing;
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();
}
The implementation of Cell::handler cannot be placed in a header file, as a forward declaration of a class vec is insufficient to allow a member function of vec to be called.

This should be enough to get you started ....
grumpy is offline   Reply With Quote
Old Jan 5th, 2006, 5:22 PM   #3
ionexchange
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 ionexchange is on a distinguished road
Thanks. I'll work with it.

Milt
ionexchange is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:35 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC