473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Can the Forward Declaration contain the infomation of inheritance ?

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 ?

Jul 23 '05 #1
1 2338

"Zeng Dinghao" <fa******@163.com> wrote in message
news:d0***********@mail.cn99.com...
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

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error:...
5
by: Simon Elliott | last post by:
For some time I've been using typedefs for STL containers for reasons outlined in: http://www.gotw.ca/gotw/046.htm However a major downside to this is that you can't forward declare a typedef...
2
by: Plok Plokowitsch | last post by:
After over a decade of programming in C++ I seem to have missed some substantial point (mental note: am I getting too old for this?). A little bit of help would be *very* appreciated. I'm trying...
3
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward...
8
by: Heiner | last post by:
Hi! The following snippet class B; class A { public:
2
by: Legendary Pansy | last post by:
Hello, I'm trying to accomplish the impossible by trying to do something equivalent of this example found here http://www.c-sharpcorner.com/Code/2003/Dec/DialogTutorial.as Starting with "Listing...
1
by: HappyHippy | last post by:
Hi, I have a question about forward declaration of classes. First of all, a simple working example, which compiles and works with no problems. ///file class_a.h:/ #ifndef __CLASS_A_H__...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
7
by: RedLars | last post by:
Need some help with a couple of questions. Previously I have seen this declaration; struct Student { int age; char name; }; and this definition of an object; struct Student stud;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.