Paul C
Paul C

Reputation: 8467

Can I access a static member function on a class/struct which I don't have any forward declaration?

There is a struct declared in a library's .cpp file and I would like to call a static member function on this struct. I have no control over this library. There is no forward declaration for it (DesiredStruct) other than

nasty.hpp (header for library):

//nasty's forward declaration
class Nasty {
    struct DesiredStruct;
    DesiredStruct  *mDesiredStruct;

    ...
}

Writing my own complete forward declaration wouldn't be feasible because theres too many #ifdefs on types and such in the definition that it would be difficult to get right.

nasty.cpp: (code in the library I have no control over)

struct Nasty::DesiredStruct {
  ... 
  public:
  static void desired_member() { ... }
}

I would like to call Nasty::DesiredStruct::desired_member(); but the compiler won't let me without a forward declaration and any attempts I have made to make a forward declaration has failed.

I don't know if it matters since desired_member is static, but I can get my hands on a pointer to Nasty.

Is there any trick I can do to make a forward declaration so I call this method?

Upvotes: 0

Views: 698

Answers (1)

jxh
jxh

Reputation: 70422

The short answer is: "No."

The provider of the library has made Nasty::DesiredStruct opaque. That is, the full declaration and definition of that structure is completely contained within the source file, and not available from a header file. This was done with the explicit purpose of not allowing you to do what you are trying to do.

Your only options are:

  • Redesign your solution so that you no longer need to call that method (it is not available to you anyway); or,
  • Convince the library implementor to expose the method in some way (perhaps with a new API).

Do NOT try to copy the method implementation into a file you do control.

Upvotes: 2

Related Questions