Jump to content
  • Advertisement
Sign in to follow this  
Mantear

Classes, inheritance, coupling

This topic is 4451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Is the CMaster class actually calling any of the methods of the objects in its collection?

Dave

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites

// 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);
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!