I would like to know if the process I'll explain below is considered as being standard c++ 98.
In fact, I have an A class that has a private member class called B. To have everything easier to read and more logical, I want to have the declaration of my A class in A.h and of my B class in B.h. As well, I want the implementation of my A class to be in A.cpp and my implementation of my B class in B.cpp.
Below are the files I used to have everything compiled (without any problem) under VS2012 and GCC. I didn't include the inclusion guards in the .h files here for readability reasons.
Thanks in advance for your help.
---------------------------- A.h --------------------------------------------
class A
{
public:
A();
void printFromB();
void createB();
private:
class B;
B* b;
};
#include "B.h"
---------------------------- B.h --------------------------------------------
#include <iostream>
class A;
class A::B
{
public:
void print();
};
---------------------------- A.cpp --------------------------------------------
#include "A.h"
A::A(){}
void A::printFromB() {
b->print();
}
void A::createB(){
b = new B;
}
---------------------------- B.cpp --------------------------------------------
#include "A.h"
using namespace std;
void A::B::print()
{
cout << endl << "B prints!" << endl;
}
-------------------------- main.cpp ------------------------------------------
#include "A.h"
int main()
{
A a;
a.createB();
a.printFromB();
return 0;
}
There is nothing illegal about your code.
By definition you will never use
class B
withoutclass A
.But then it is that very definition which asks the question, "Then why bother creating a separate file?" Since this is your project though, you do it how you want.