2

I'm looking at the headers of a C++ library I use and there's a line like this:

typedef struct asdf_t asdf_t;

There is no definition of asdf_t anywhere in the source. What is this line doing?

polm23
  • 14,456
  • 7
  • 35
  • 59

2 Answers2

4

This is what's know as declaring an Opaque Structure! There are probably functions and/or classes in that header which use pointers to the asdf_t type - which is allowed in C++ for 'undefined' structures. However, there won't (can't) be variables, class members or function arguments declared that are instances of asdf_t.

So, for example, your header may later declare the following class:

class GoodBoy {
public: GoodBoy();
       ~GoodBoy();
private:
    asdf_t* forMyEyesOnly; // Only a pointer - definition not needed!
}

But it cannot have this:

class BadGirl {
public BadGirl();
      ~BadGirl();
private:
    asdf_t notForAnyOneToSee; // Won't compile - would need definition.
}

Why is this done?

Normally, it's because the creators of the library you are using don't want you to know the details of that structure! However, they have to provide such a 'minimial' definition, so that you can use classes, et cetera, that - by necessity - use or reference that structure: So, for example, in the implementation of the GoodBoy class (i.e. private source files, in which the full definition is given for asdf_t), one could have the constructor and destructor doing something like, say:

GoodBoy::GoodBoy() {
    forMyEyesOnly = new asdf_t; // Note: Neither the 'new' operation, nor...
}
GoodBoy::~GoodBoy() {
    delete forMyEyesOnly; // ...the 'delete' can compile without FULL DEFINITION!
}

Clarification

Normally, such an opaque structure is declared simply by: struct asdf_t;! However, both typedef struct asdf_t asdf_t; and typedef struct asdf_t; are also valid syntax (if a little weird).

Further Reading

There's a good (if a little old) Stack Overflow discussion on opaque structures, and how to declare them, here.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    Note, too, that this is a C hack. In C++ `struct asdf_t;` is sufficient. – Pete Becker Oct 07 '19 at 13:18
  • @PeteBecker Indeed - On the simpler form, check what I wrote under "Clarifications." On the C vs C++ syntax rules: I'll admit my lack of precise understanding, and I shall bow to your attestation. – Adrian Mole Oct 07 '19 at 14:03
2

It's a forwarded declaration, basically you can use this type also if has no definition (so you can't do anything that require a knowledge of his exactly definition for example you can't copy because the compiler must know his size).

  • For example you could define some function prototype in the same header files.
  • You can define a pointer and copy/pass this pointer (without accessing his data).

This is also know as ADT (https://en.wikipedia.org/wiki/Abstract_data_type)

more in detail you could do things like that and compiler (and programmers) will know that ADT* it's a type different from example to char*:

typedef struct adt ADT;
void swap(ADT **a, ADT **b) {
   ADT *tmp = *a;
   *a = *b;
   *b = tmp;
}
Manu-sh
  • 114
  • 4