Discussion:
Forward declarations and namespaces
(too old to reply)
whithers
2004-01-16 06:25:40 UTC
Permalink
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
David Fisher
2004-01-16 06:41:17 UTC
Permalink
Post by whithers
How do I do forward declarations when namespaces are involved?
You can just say:

namespace MyNameSpace
{
class A;
};

David F
Sumit Rajan
2004-01-16 06:57:12 UTC
Permalink
Post by whithers
How do I do forward declarations when namespaces are involved? For
-------------------------------------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.
Sumit Rajan
2004-01-16 07:12:09 UTC
Permalink
Post by whithers
Post by whithers
How do I do forward declarations when namespaces are involved? For
-------------------------------------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;
The "MyNameSpace::" part is not really neccessary in the above line -- just
"A* m_pAinst;" would do the job.
Post by whithers
Post by whithers
};
};
#endif
How do I declare A so I don't have to include the header classA.h in
classB.h
thanks
//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.
E. Robert Tisdale
2004-01-16 06:39:42 UTC
Permalink
Post by whithers
How do I do forward declarations when namespaces are involved?
-------------------------------------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!
Loading...