Subclassing and include headers problem



 DEVELOP > c-Plus-Plus > Subclassing and include headers problem

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 02 Feb 2008 09:52:06 PM
Object: Subclassing and include headers problem
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
.

User: "Alf P. Steinbach"

Title: Re: Subclassing and include headers problem 02 Feb 2008 09:56:37 PM
* gordon.is.a.moron@gmail.com:

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
--
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?
.
User: ""

Title: Re: Subclassing and include headers problem 03 Feb 2008 02:20:12 AM
On Feb 3, 3:56 pm, "Alf P. Steinbach" <al...@start.no> wrote:

* gordon.is.a.mo...@gmail.com:



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.,

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.
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER