class structure question

Is there a sensible way to achieve something like this:

An instance of class M instantiates one of class S-1 and one of class S-2, and...
I want S-1 to be able to call methods of M,
S-2 to be able to call methods of S-1,
and M to be able to call methods of both S-1 and S-2.

These classes are otherwise unrelated -- no inheritance.

Alternatively, I could combine S-1 into M (just replace the S-1 methods and data with M methods and data) but then I would need S-2 (which is declared by M) to be able to call M methods (and M to be able to call S-2 methods).

Well, obviously M can call the (public) methods (or rather, member functions) of S-1 and S-2. To allow S-1 to call the (public) member functions of M, you may need to pass a reference or pointer to the M object to the S-1 object instantiated. For S-2 to call the (public) member functions of S-1, you may need to pass a reference or pointer to the S-1 object.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Yes, I forgot to mention that I was hoping to avoid that. Is there any way to avoid that?

Or can you think of any other (non-class?) structure to replace the S-1 class that would serve as an interface making member functions of M accessible from other classes that are instantiated by M.

The point is that everything in M is (robot) platform-specific and provides access to the hardware. S-2, etc, would contain code that should be portable to other platforms (given appropriate interfaces). The OS requires that M be the primary object which instantiates S-2, and not the other way around.

The point is that everything in M is (robot) platform-specific and provides access to the hardware. S-2, etc, would contain code that should be portable to other platforms (given appropriate interfaces).

What's S-1 for?

The OS requires that M be the primary object which instantiates S-2, and not the other way around.

That's fine though. As I mentioned, you can pass a reference or pointer to M to S-2.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

An instance of class M instantiates one of class S-1 and one of class S-2, and...
I want S-1 to be able to call methods of M,
S-2 to be able to call methods of S-1,
and M to be able to call methods of both S-1 and S-2.

These classes are otherwise unrelated -- no inheritance.

Alternatively, I could combine S-1 into M (just replace the S-1 methods and data with M methods and data) but then I would need S-2 (which is declared by M) to be able to call M methods (and M to be able to call S-2 methods).

you could use pointers to member functions
or declare M *m as a member of S-1
and S-1 *s1 as a member of S-2
and S-1 *s1 and S-2 *s2 as members of M

I still had to #include the s_1 and s_2 headers in m's header file. When I tried with forward declarations I got "incomplete type" errors for m's 'one' and 'two' data members.

But the other two headers were ok with just forward declarations, and now the whole thing compiles & runs correctly.

Thanks for the help.

it sounds like you're delcaring S_1 s1 and not S_1 *s1.

you have to use a pointer in forward declarations. this works because pointers are always 32bts, but when declaring an actual instance of the class as a member, it has to know exactly how much memory to allocate.

be sure to split your classes into h files, containing only declarations, and cpp files containing the implementations, and do your #includes to any non-descendant (you can't do a forward declaration for a base class) classes in the cpp file.