Forward declarations and namespaces

W

whithers

How do I do forward declarations when namespaces are involved? For
example, given the following:

-------------------------------------ClassA.h
#ifndef CLASSA_H
#define CLASSA_H

namespace MyNameSpace
{
class A{};
};

#endif


--------------------------------------ClassB.h
#ifndef CLASSB_H
#define CLASSB_H

class A;

namespace MyNameSpace
{
class B
{
MyNameSpace::A* m_pAinst;
};

};

#endif


How do I declare A so I don't have to include the header classA.h in
classB.h

thanks
 
E

E. Robert Tisdale

whithers said:
How do I do forward declarations when namespaces are involved?
For example, given the following:

-------------------------------------ClassA.h
#ifndef CLASSA_H
#define CLASSA_H 1

namespace MyNameSpace {
class A{};
};

#endif//CLASSA_H


--------------------------------------ClassB.h
#ifndef CLASSB_H
#define CLASSB_H 1

namespace MyNameSpace {
class A;
class B {
A* m_pAinst;
};
};

#endif//CLASSB_H


How do I declare A so that
I don't have to include the header classA.h in classB.h?

Gasp!
 
S

Sumit Rajan

whithers said:
How do I do forward declarations when namespaces are involved? For
example, given the following:

-------------------------------------ClassA.h
#ifndef CLASSA_H
#define CLASSA_H

namespace MyNameSpace
{
class A{};
};

#endif


--------------------------------------ClassB.h
#ifndef CLASSB_H
#define CLASSB_H

class A;

namespace MyNameSpace
{
class B
{
MyNameSpace::A* m_pAinst;
};

};

#endif


How do I declare A so I don't have to include the header classA.h in
classB.h

thanks

Try this:
//B.h
#ifndef CLASSB_H
#define CLASSB_H



namespace MyNameSpace {
class A;
class B {
MyNameSpace::A* m_pAinst;
};

}

#endif

//test.cpp
#include "B.h"

int main()
{
MyNameSpace::B b;
}


Regards,
Sumit.
 
S

Sumit Rajan

The "MyNameSpace::" part is not really neccessary in the above line -- just
"A* m_pAinst;" would do the job.
 

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

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top