Re: Inheritance

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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

.



Relevant Pages

  • Re: When is a static data member defined?
    ... If you just declare a function ... it will compile and link fine as long as you don't ... to implement a language in general. ... memory and a pointer to that table get stored in every object of that ...
    (comp.lang.cpp)
  • Re: Including headers in my project
    ... > Hi muchan, ... compile only the main.cpp and see if it causes error or warnings. ... or inconsistency of these keywords may cause multiple definitions... ... so check the line number of that error, then see what type it try to declare, ...
    (microsoft.public.vc.language)
  • Re: Pointer corruption calling across a DLL boundary
    ... >> Make sure they are compiled using the same compile option. ... which respectively declare a pointer to a pointer and an ... array of indeterminate size of pointers. ... the workaround is to declare a local copy of the ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Pointer corruption calling across a DLL boundary
    ... >> Make sure they are compiled using the same compile option. ... which respectively declare a pointer to a pointer and an ... array of indeterminate size of pointers. ... the workaround is to declare a local copy of the ...
    (microsoft.public.vstudio.development)
  • Re: Inheritance
    ... class a: public CObject ... I would not define that as "multiple inheritance"... ... The proble starts when, in class b, I declare a pointer ... This will not compile with out me including the header file c.h. ...
    (microsoft.public.vc.mfc)