0

A Simple Example:

void foo(class Bar * bar) {
    foo2(bar);
}

void foo2(Bar * bar) {
    bar->a++;
}

The Pointer used in foo2 is a standard Pointer to the class Bar. OK - The Pointer used in foo is also a pointer to the class Bar. But the class Bar must not be known at this place.

I can't find the correct naming for the argument of foo. It is a pointer to an anonymous class? What is the correct name for the class Bar *bar?

Mixhab
  • 421
  • 3
  • 14
powerpete
  • 2,663
  • 2
  • 23
  • 49
  • 2
    What, you mean like a *forward declaration*? – Some programmer dude Dec 20 '17 at 09:37
  • 2
    Your question is unclear, especially the declaration of `Bar` is missing. Please post a [mcve] – Jabberwocky Dec 20 '17 at 09:37
  • @Michael Walz I don't think the declaration of `Bar` is relevant for this question. – Detonar Dec 20 '17 at 09:40
  • It's also a "standard" pointer to `Bar`. – molbdnilo Dec 20 '17 at 09:44
  • 1
    Nothing special about `foo()`. If `class Bar` is forward declared (i.e. incomplete class e.g. `class Bar;`) then `foo2(bar);` is valid but `bar->a++;` not. That is because incomplete types may be used as pointer only. – Scheff's Cat Dec 20 '17 at 09:45
  • @Detonar Of course it is. We need to know what _"the class Bar must not be known at this place"_ is supposed to mean - presumably that they only forward-, not fully, declared the class - but it's not useful without full clarity on what is being asked. – underscore_d Dec 20 '17 at 09:47
  • @Scheff Incomplete types can also be used by reference and as the return/argument types in function declarations. – underscore_d Dec 20 '17 at 09:48
  • Possible duplicate of [When can I use a forward declaration?](https://stackoverflow.com/questions/553682/when-can-i-use-a-forward-declaration) – underscore_d Dec 20 '17 at 09:49
  • @underscore_d Yepp. And all this is nicely explained in the link which is provided in the answer of [Mixhab](https://stackoverflow.com/a/47903014/7478597). I would've been even better if he had quoted the relevant parts... – Scheff's Cat Dec 20 '17 at 09:52
  • probably a better duplicate: [Why is a forward declaration in a function declaration allowed?](https://stackoverflow.com/questions/31422807/why-is-a-forward-declaration-in-a-function-declaration-allowed) – underscore_d Dec 20 '17 at 10:27

2 Answers2

3

What you need is forward declaration.

You can read about it in the documentation or in a different post.

From the documentation:

Declares a class type which will be defined later in this scope. Until the definition appears, this class name has incomplete type. This allows classes that refer to each other.

Mixhab
  • 421
  • 3
  • 14
  • The link to cppreference.com is appropriate. You could improve your answer by copying and quoting the relevant parts from there. (And there is a typo: "codumentation" instead of "documentation" - though it's funny...) – Scheff's Cat Dec 20 '17 at 09:53
  • Thanks for the comment @Scheff. I added a "teaser" from the documentation. I personally like concise answers in this site and add reference for whoever want to broaden their horizons , especially when those 2 links have very good explanations. – Mixhab Dec 20 '17 at 10:02
  • When I was new in SO (not so long ago) somebody taught me that quoting should be preferred (and links may be added additionally) as "links get rotten over time". That's true. Over time, I found some good answers where links were broken... – Scheff's Cat Dec 20 '17 at 10:11
2

I would call this an inline forward class declaration but i don't think there is a special name for this.

It just tells that there is a class Bar so it can be used in this scope.

You CAN'T call members of Bar with just this. all it says is there is, it doesn't says what it is. Bar is a incomplete type like this.

Detonar
  • 1,409
  • 6
  • 18