Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward declaration within a class (not a nested class)

I came across this odd forward declaration (or something) where instead of having a normal:

class A;
class B{
    A* var;
}

You can do

class B{
   class A* var;
}

Now this might be an elaborated type specifier, but I'm not sure about that. I've tried it myself and I had no issues, even made my code a lot more cleaner, but I'm afraid it may cause scoping issues that I'm currently unaware of.

Does anyone have an insight on this? is this a valid forward deceleration?

example: https://youtu.be/EIptJ0YrYg0?t=412

like image 757
ofir agranat Avatar asked Sep 04 '26 17:09

ofir agranat


1 Answers

Note that the doc says:

Note that a new class name may also be introduced by an elaborated type specifier which appears as part of another declaration, but only if name lookup can't find a previously declared class with the same name.

Thus, if class B is enclosed in a namespace and no previous forward declaration of A is performed, then it refers to n2::A that will have to be defined.

like image 115
nop666 Avatar answered Sep 07 '26 07:09

nop666



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!