Can the Forward Declaration contain the infomation of inheritance ?

Z

Zeng Dinghao

I encouter this problem.
here are the sample files:

//--------------- File: Bar.h ------------------------------
class Bar : public BarBase {
public:
void BarFunc(); // there is no BarFunc in Barbase
};

//----------------------------------------------------------

//--------------- File: Bar.cpp ----------------------------
void Bar::BarFunc() {
}

//----------------------------------------------------------

//----------------File: Client.h ---------------------------

class Bar; // ! Forward Declaration !
// I don't include "Bar.h", since
// I want to decrease the dependency

void Foo(BarBase* pBarBase); // defined in other .cpp file

class Client {
public:
// ... some other declaration
template<class>
void TemplFunc(T& data) {
// do sth. about data
Foo(m_pBar);
}

protected:
Bar* m_pBar;
};

//-----------------------------------------------------------

//----------------File: Client.cpp---------------------------

#include "Bar.h" // I include it here

Client::Client() {
m_pBar = new Bar();
m_pBar->BarFunc();
}
//-----------------------------------------------------------



The compiler complained that
can not convert type "Bar*" to type "BarBase*"
when calling "void Foo(BarBase* pBarBase)" in "TemplFunc"

How can I get through without including "Bar.h" in "Client.h" ?
I insist this because there are many files need to include "Client.h".

In other word, can the Forward Declaration contain the infomation of
inheritance ?
 
H

Howard

Zeng Dinghao said:
I encouter this problem.
here are the sample files:

//--------------- File: Bar.h ------------------------------
class Bar : public BarBase {
public:
void BarFunc(); // there is no BarFunc in Barbase
};

//----------------------------------------------------------

//--------------- File: Bar.cpp ----------------------------
void Bar::BarFunc() {
}

//----------------------------------------------------------

//----------------File: Client.h ---------------------------

class Bar; // ! Forward Declaration !
// I don't include "Bar.h", since
// I want to decrease the dependency

void Foo(BarBase* pBarBase); // defined in other .cpp file

class Client {
public:
// ... some other declaration
template<class>
void TemplFunc(T& data) {
// do sth. about data
Foo(m_pBar);
}

protected:
Bar* m_pBar;
};

//-----------------------------------------------------------

//----------------File: Client.cpp---------------------------

#include "Bar.h" // I include it here

Client::Client() {
m_pBar = new Bar();
m_pBar->BarFunc();
}
//-----------------------------------------------------------



The compiler complained that
can not convert type "Bar*" to type "BarBase*"
when calling "void Foo(BarBase* pBarBase)" in "TemplFunc"

How can I get through without including "Bar.h" in "Client.h" ?
I insist this because there are many files need to include "Client.h".

In other word, can the Forward Declaration contain the infomation of
inheritance ?

I don't see how including Bar.h has any bearing on the problem. Nor does
the forward declaration.

I think all you need to do is either declare m_pBar as a BarBase* instead of
a Bar*, or else cast m_pBar as a BarBase* when calling Foo.

-Howard
 

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
474,202
Messages
2,571,057
Members
47,660
Latest member
vidotip479

Latest Threads

Top