Classes, inheritance, coupling

Started by
4 comments, last by Zahlman 17 years, 11 months ago
Greetings, Using C++/MSVS 2005. I'm having trouble setting up three classes. If I'm doing something fundamentally wrong, please point it out. Because of the trouble I'm having, I imagine I am. I've got a class called CMaster. CMaster will have many slave objects. Each slave object will have some basic similarities covered by a base class for the slaves. We'll call these CBaseSlave and CSlave*, where CSlave* inherits from CBaseSlave. The problem comes in when I want the CSlave* objects to be able to call functions on CMaster. That idea right there is where I think I'm going wrong, but I can't really find a better method right now. In order to do that, I've tried including a pointer to CMaster in CBaseSlave and I've also tried inlcuding a pointer to CMaster in each CSlave*, giving the pointer of CMaster to the CSlaves* in their constructors. But I always run into dependancy issues. The best I've been able to do (after many what appear to be excessive includes and class forward declarations) is to get warnings about trying to delete pointers to incomplete classes and that the destructors will not be called. Thank you.
Advertisement
Is the CMaster class actually calling any of the methods of the objects in its collection?

Dave
Yes. The CMaster class will instantiate the CSlave* classes, and call their methods. At times, I want the CSlave* classes to be able to call a function on the CMaster class.

An example would be if CMaster wanted to whip() all of its CSlave*s. One of the CSlave*s may need to call fainted() on CMaster to tell CMaster that something happened. I know that in this simplified example I could just have a return value to the whip() function, but it's a little to complicated to have a simple return value. I really want to be able to call fainted() on the CMaster.
Forward declarations are correct ... and the ONLY forward declaration that should be needed is in the CSlaveBase header ... which should need:

class CMaster;

right after any #includes, and before the class.

Do not include the CMaster header file in either of the slave headers ... DO include the CMaster header in the slave cpp files.

refer to the article on C++ header / file organization on this site.
That a side, calling your classes Master and Slave is a bit controversial:
http://www.google.nl/search?hl=nl&q=master+slave+lawsuit&meta=

Most people call the classes Parent and Child.
// Of course, not everything possible is shown...class Child;class Parent {  vector<Child*> children;  public:  void sigh() {}  void order(int who) {    children[who]->doSomething();  }  void adopt(bool gender);  ~Parent() {    for (vector<Child*>::iterator it = children.begin();         it != children.end(); ++it) { delete *it; }    }  }  Parent(const Parent& other) {    for (vector<Child*>::iterator it = other.children.begin();         it != other.children.end(); ++it) {      children.push_back((*it)->clone());    }  }  Parent& operator&(const Parent& rhs) {    Parent other(rhs);    children.swap(other.children);  }};class Child {  Parent* p;  public:  virtual void doSomething() = 0;  void complain() { p->sigh(); }  virtual Child* clone() = 0;  virtual ~Child() = 0;};class Sally: public Child {  public:  void doSomething() { mow_lawn(); }  Sally* clone() { return new Sally(); }  ~Sally() { /* whatever is needed here. */ }};class Billy: public Child {  public:  void doSomething() { wash_dishes(); complain(); }  Billy* clone() { return new Billy(); }  ~Billy() { /* whatever is needed here. */ }};// Now that both children are defined, this is possible:void Parent::adopt(bool gender) {  Child* c = gender ? new Sally() : new Billy();  children.push_back(c);}

This topic is closed to new replies.

Advertisement