Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward declare pointers-to-structs in C++

I am using a 3rd party library that has a declaration like this:

typedef struct {} __INTERNAL_DATA, *HandleType;

And I'd like to create a class that takes a HandleType in the constructor:

class Foo
{
    Foo(HandleType h);
}

without including the header that defines HandleType. Normally, I'd just forward-declare such a type, but I can't figure out the syntax for this. I really want to say something like:

struct *HandleType;

But that says "Expected identifier before *" in GCC. The only solution I can see is to write my class like this:

struct __INTERNAL_DATA;
class Foo
{
    Foo(__INTERNAL_DATA *h);
}

But this relies on internal details of the library. That is to say, it uses the name __INTERNAL_DATA, which is an implementation detail.

It seems like it should be possible to forward-declare HandleType (part of the public API) without using __INTERNAL_DATA (part of the implementation of the library.) Anyone know how?

EDIT: Added more detail about what I'm looking for.

like image 425
Jesse Rusak Avatar asked Mar 01 '23 22:03

Jesse Rusak


2 Answers

Update:

I am using it in the implementation .cpp of Foo, but I want to avoid including it in my header .h for Foo. Maybe I'm just being too pedantic? :)

Yes you are :) Go ahead with forward declaration.

If HandleType is part of the interface there must be a header declaring that. Use that header.

Your problem is still a vague one. You are trying to protect against something you cannot.

You can add the following line to your client library:

typedef struct INTERNAL_DATA *HandleType;

but, if the name/structure changes you may be in for some casting nastiness.

Try templates:

template <class T>
class Foo
{
    Foo(T h);
};

Forward declaration is fine. If you are going to use pointers or references you only need a class (__INTERNAL_DATA) declaration in scope. However, if you are going to use a member function or an object you will need to include the header.

like image 147
dirkgently Avatar answered Mar 11 '23 03:03

dirkgently


If the type is in a 3rd party library then the big benefit of forward declaration (isolating rebuilds due to changes in headers) is effectively lost.

If you're worried about compilation times (it's a sizeable header) then perhaps you can place it in a precompiled header or just include the relevant header from a library.

E.g. many library headers look like

// library.h
#include "Library/Something.h"
#include "Library/SomethingElse.h"
like image 43
Andrew Grant Avatar answered Mar 11 '23 03:03

Andrew Grant