C++ Trying to Find An Alternative Way to Implement Forward Declaration

29 views Asked by At

I asked this question 2 days ago, but I had to fix some lines. So here my question again:

I have multiple classes and have written them in an order. First class has an enum, but I want to move it to the class that is below all other classes. This is where problem begins. Since the last class will have that enum, members that use enum in first class gives error.

Here is the initial code:

file.hpp:

class A
{
public:
    typedef enum
    {
        eOk = 0x00;
        eError = 0x01;
    }myEnum_et;

    typedef myEnum_et(*callbackType)(B*);
    callbackType myCallback;

public:
    void foo1(callbackType callbackParam)
    {
        myCallback = callbackParam;
    }

    myEnum_et foo2()
    {
        return myCallback;
    }
};

class B
{
public:

    A::myEnum_et foo3(A *a)
    {
        if(a->foo2() != A::eOk) return A::eError;
        else return A::eOk;
    }
};

class C
{
public:
    B *b;

    void foo4(B *new_b)
    {
        b = new_b;
    }    
};

What I want to do is:

class C;
class B;

typedef C::myEnum_et(*callbackType)(B*);

class A
{
public:
    callbackType myCallback;

public:
    void foo1(callbackType callbackParam)
    {
        myCallback = callbackParam;
    }

    C::myEnum_et foo2()
    {
        return myCallback;
    }
};

class B
{
public:

    C::myEnum_et foo3(A *a)
    {
        if(a->foo2() != C::eOk) return C::eError;
        else return C::eOk;
    }
};

class C
{
public:
    typedef enum
    {
        eOk = 0x00;
        eError = 0x01;
    }myEnum_et;

    B *b;

    void foo4(B *new_b)
    {
        b = new_b;
    }    
};

As seen above, I just want to move myEnum_et from class A to class C and move function pointer to out of class A's scope. As you know, declaring class C at the top of code is not enough for those that will use myEnum_et. I guess the reason is that compiler needs full definition of class C. Here is the error :

#71 Incomplete type is not allowed.

I am aware that code's readability is ugly. I just wanted show that all classes' definition depend on each other and want to use forward declaration(or other solutions) at this class order. Classes' methods may not make any sense, my purpose is just to implement forward declaration somehow. By the way, I use C++03 so I am kinda limited.

0

There are 0 answers