Subclassing and include headers problem

  • Thread starter gordon.is.a.moron
  • Start date
G

gordon.is.a.moron

Hello,

I have what I suppose is a simple problem, but I'm not sure the proper
way to fix it. I have two classes, one which I want to derive from
another (the first one which is derived from a third party library
class):


//MyClassA.h

#include"MyClassB.h"

class MyClassA : public LibraryClass
{

friend class TestSuite; //just for test rig

public:
MyClassA(void);
~MyClassA(void);

private:
MyClassB* myb_;
};

The trouble is, it doesn't know about MyClassB unless I add the
include directive for MyClassB in this header (I am using #pragma once
to avoid header file redefinitions, I was using include guards
before).
And I want to derive MyClassB from MyClassA because I need access to
the third party library macros:

#include "MyClassA.h"

class MyClassB: public MyClassA
{

friend class TestSuite; // for test rig

public:
MyClassB(void);
~MyClassB(void);

private:
void LibFunc(libStuff);

};


So the problem is basically ClassA needs to see ClassB and vice versa.
What I have done for now is simply take the MyClassB* myb_ member out
of the MyClassA.h header file and put it into the MyClassA.cpp
implementation file. However this seems a bit untidy to me, as I
thought the class declarations should stay in the header. I'm assuming
there is a better way. Any ideas?

Regards,
Gordy
 
A

Alf P. Steinbach

* (e-mail address removed):
Hello,

I have what I suppose is a simple problem, but I'm not sure the proper
way to fix it. I have two classes, one which I want to derive from
another (the first one which is derived from a third party library
class):


//MyClassA.h

#include"MyClassB.h"

class MyClassA : public LibraryClass
{

friend class TestSuite; //just for test rig

public:
MyClassA(void);
~MyClassA(void);

private:
MyClassB* myb_;
};

The trouble is, it doesn't know about MyClassB unless I add the
include directive for MyClassB in this header (I am using #pragma once
to avoid header file redefinitions, I was using include guards
before).

You can just forward-declare

class MyClassB;

before the MyClassA definition.

And I want to derive MyClassB from MyClassA because I need access to
the third party library macros:

Access to macros has nothing to do with class derivation.


Cheers, & hth.,

- Alf
 
G

gordon.is.a.moron

* (e-mail address removed):







You can just forward-declare

class MyClassB;

before the MyClassA definition.


Access to macros has nothing to do with class derivation.

Cheers, & hth.,

Yes it did help, thanks Alf. This must be the forward declaration
issue that I've heard about then. As for the macros, I should explain
(though I could be wrong anyway) that they are used by the library
class, so I have to subclass it directly or via my derived class,
IYSWIM. I have to pass my derived class into the macro, to set up the
libraries events system.


Much obliged,

Gordy.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top