Search code examples
c++forward-declaration

how to create a forward declaration of a typedef struct


I have 1 .h file test.h containing a class. In this class there's a private method returning a pointer on a type I don't want to make "public", but I want to be able to include this test.h file in other source files.

In general, it's easy to use a forward declaration in the .h file:

class Foo;

But the problem is that this type comes from a C file that I cannot change (because it's someone else code that I'm not maintaining) and it's a typedef.

So basically my test.cpp is:

// this type comes from a C file, cannot be changed to struct or class
typedef struct 
{
   int x;
} Foo;

#include "test.h"

static Foo foo;

Foo *Test::private_function()
{
  foo.x = 12;
  return &foo;
}

int Test::compute()
{
   auto *local_foo = private_function();
   return local_foo->x;
}

and my test.h file is:

#pragma once

struct Foo;

class Test
{
public:
  Test() {}
  int compute();
private:
  Foo *private_function();
};

trying to compile that fails:

>g++ -std=c++11 -c test.cpp
In file included from test.cpp:10:0:
test.h:3:8: error: using typedef-name 'Foo' after 'struct'
test.cpp:7:3: note: 'Foo' has a previous declaration here

Currently my workaround is to return void * and perform static_cast back and forth, but I don't find that optimal. Is there a nicer solution?

(I have checked Forward declaration of a typedef in C++ but I tested the solutions and they don't seem to work in my case, maybe what I want to do is simpler/different - I have just a .h and .cpp - or just not possible)


Solution

  • Unfortunately typedefs cannot be forward-declared.

    A common workaround is to have a C++ class that inherits from the C struct, referenced by its typedef, and you can forward-declare that. This will require some code changes, but they should be minimal.

    (Posting on behalf of https://stackoverflow.com/users/3943312/sam-varshavchik who commented)