Two base classes need Inheritance?

I

Immortal Nephi

I write three classes. The name of class are A, B, and C. Class A
and Class B are inaccessible to the client. Class C is accessible to
the client.

Class B has inheritance to Class A. The member function in class B
modify data in Class A. I need to find a way how Class A can have
inheritance to Class B, but C++ Compiler does not allow it. The rule
of inheritance goes from top to the bottom.

Class A needs to have inheritance to Class B. The member function in
class A modify data in Class B.

It is the way how Class A and Class B can become one class to modify
both Class A's and Class B's data together. Class A is getting too
large over 200,000 lines. It makes easier to divide one class into
two classes for better readable C++ writing.

Class C is derived from both Class A and Class B. Here is an example
how three classes look like.

A-->B A<--B
A-------------B
\ /
\ A and B /
\ Go /
\ Down /
\ To /
\ C /
\ /
|
C

class A;
class B;
class C;

class A : // C++ Compiler disallows....
public B // class A modifies class B's data
{...};

class B : // C++ Compiler allows....
public A // class B modifies class A's data
{...};

class C :
public A, public B // class C displays both classes' data
{...};

Can you please write an example of short source code? Find a way how.

Immortal Nephi
 
I

Immortal Nephi

* Immortal Nephi:


http://en.wikipedia.org/wiki/God_object

Your request is about a technical way to allow even more spaghetti.

Perhaps you can understand why that is not a good idea?

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

You may have heard diamond inheritance. It is an example of ios,
istream, ostream, and iostream. You need to think how to use triangle
inheritance. Only iostream is able to communicate to istream directly
and ostream directly. Unfortunately, istream has no way to
communication to ostream directly and ostream has no way to
communication to istream.

It is the only way that istream can access data inside iostream
class. In turn, ostream can access data inside iostream class AFTER
data is already MODIFIED by istream.

I wish that C++ Compiler should be able to implement triangle
inheritance. Please advise.

Nephi
 
C

Chris M. Thomasson

[...]
Bleah, I wasn't thinking above. Just use a static_cast:

class B;

class A {
private:
B* b();
void f1();
A() { } // private to ensure all A objects are really B objects
friend class B;
};

class B : public A {
public:
void f2() { this->f1(); }
};

inline B* A::b() { return static_cast<B*> (this); }

void A::f1() { b()->f2(); }

Won't invoking B::f2 go into and endless loop and blow the stack?

;^)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top