Domanda

I've been always avoiding the following in C++ (which I believe is C++03 used in VS 2008) but now I'm curious if it's possible to do this? Let me explain it with the code.

//Definitions.h header file
//INFO: This header file is included before CMyClass definition because
//      in contains struct definitions used in that class

struct MY_STRUCT{
    void MyMethod()
    {
        //How can I call this static method?
        int result = CMyClass::StaticMethod();
    }
};

then:

//myclass.h header file
#include "Definitions.h"

class CMyClass
{
public:
    static int StaticMethod();
private:
    MY_STRUCT myStruct;
};

and:

//myclass.cpp implementation file

int CMyClass::StaticMethod()
{
    //Do work
    return 1;
}
È stato utile?

Soluzione

In this case, you would need to move the implementation of MY_STRUCT::MyMethod outside the header file, and put it somewhere else. That way you can include Definitions.h without already having CMyClass declared.

So your Definitions.h would change to:

struct MY_STRUCT{
    void MyMethod();
};

and then elsewhere:

void MY_STRUCT::MyMethod()
{
    int result = CMyClass::StaticMethod();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top