Re: Inheritance
- From: Al <Al@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 28 Mar 2007 06:42:01 -0700
Yes I think you are right about it being a bad design. But I am trying to fix
that.
If you have a class day,
A class hour derived from day,
And a class minute derived from hour
Now I want to initiate a couple on objects of type hour, let say hour(1) &
hour(2). Then later I wan to initiate a couple of objects of type minute,
derived from type hour(1) and of type hour(2). Of course there will be
individual info in each of these objects.
What is the best way to do this? I was going to use a pointer array but the
way I have tried does not work.
Do I save these all, as a type day?
And if I want to initiate an object of type minute derive from a object of
type hour(1). What is the best way to do that?
--
Just Al
"MrAsm" wrote:
On Tue, 27 Mar 2007 18:55:07 -0700, Al <Al@xxxxxxxxxxxxxxxxxxxxxxxxx>.
wrote:
I am having a problem using multiple inheritances. If I have 3 classes;
class a : public CObject
class b : public a
class c : public b
I would not define that as "multiple inheritance"... it's just single
inheritance; the inheritance tree for your classes would be something
like this:
CObject
^
|
|
+---+
| a |
+---+
^
|
|
+---+
| b |
+---+
^
|
|
+---+
| c |
+---+
Multiple inheritance is something like:
class c : public a, b
+---+ +---+
| a | | b |
+---+ +---+
^ ^
| |
+----+----+
|
|
+---+
| c |
+---+
Now in class a, I declare a pointer CTypePtr<CObArray, b> m_bArray; I have
to include the b.h file to declare this pointer.
This seems to compile and work.
The proble starts when, in class b, I declare a pointer
CTypePtr<CObArray, c> m_cArray;
This will not compile with out me including the header file c.h. When I add
this header file and compile I get about 368 errors. I believe the problem is
right here but I don’t know enough to figure it out. Could someone please
help? I get the feeling that I am trying to something really wrong.
It would be intereseting to see some errors...
As others wrote, have you tried #pragma once?
Moreover, in b.h, have you tried forward declaration "class c;" ?
I would like to have a object a that is the base class of b & c. Add some
info into b(1) that would have multiples of c. And have another object b(2)
with multiples of c again. What is the right way to do this, please help.
This seems different from your original class hierarchy...
I would do something like this:
CObject
^
|
|
+---+
| a |
+---+
^
|
|
+----+----+
^ ^
| |
| |
+---+ +---+
| b | | c |
+---+ +---+
class a : public CObject
class b : public a
class c : public a
in class b you can reference multiple instances of c, e.g. storing an
array of pointers to c
Have you tryed to replace the MFC CTypedPtrArray with std::vector?
i.e. in class b you may add a data member that is a vector containing
pointers to c's:
class b
{
...
std::vector< c * > m_cArray;
};
MrAsm
- Follow-Ups:
- Re: Inheritance
- From: Joseph M . Newcomer
- Re: Inheritance
- From: MrAsm
- Re: Inheritance
- References:
- Re: Inheritance
- From: MrAsm
- Re: Inheritance
- Prev by Date: Re: Avoiding Multiple Instances of an Application using CreateMutex
- Next by Date: Connection Manager API problem
- Previous by thread: Re: Inheritance
- Next by thread: Re: Inheritance
- Index(es):
Relevant Pages
|