I am developing a series of event classes. These classes contain information acquired from a system. They are from different natures and may contain different messages, for example: one possible event will contain a huge list of number, while another will carry query info in form of strings. I would like to create a builder function that creates the event of the correct type and returns to the user.
Here is a demonstration of how it will look like:
main.cpp:
#include <iostream>
#include "A.h"
#include "dA.h"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
A a = build(5);
return 0;
}
Event.h:
#ifndef A_H_INCLUDED
#define A_H_INCLUDED
#include <iostream>
class A
{
public:
friend A build(int x);
protected:
A(int x);
private:
};
#endif // A_H_INCLUDED
event.cpp:
#include "A.h"
A::A(int x)
{
{
std::cout << "A\n";
std::cout << x << std::endl;
}
}
A build(int x)
{
if(x == 5)
return dA(x);
return make(x);
}
Special event.h
#ifndef DA_H_INCLUDED
#define DA_H_INCLUDED
#include <iostream>
#include "A.h"
class dA : public A
{
public:
protected:
dA(int x);
};
#endif // DA_H_INCLUDED
special event.cpp:
#include "dA.h"
dA::dA(int x) : A(x)
{
std::cout << "dA\n";
}
As you can see, all the constructors are protected, the intent is to hide the construction from the user - (s)he should not create events from nothing, only the system can - and to guarantee that build function will return the correct type of event.
When compiling, I get:
error: 'build' was not declared in this scope
warning: unused variable 'a' [-Wunused-variable]
If I put every thing in the same file, I can compile, however, I end up with a huge file difficult to manage. Note that the above code is just an example, the real class I will use are huge, putting all in the same file, both declaration and implementation, everything becomes a mess.