1

I was through the source of wxWidgets when I saw this code

class WXDLLIMPEXP_FWD_CORE wxKeyEvent;

After this they defined a class like this

class WXDLLIMPEXP_FWD_CORE wxKeyEvent : public wxEvent{
//some code
};

As you can see there is a white space, but you cannot have it while naming the class, further I wrote this small code which compiles successfully `

#include <bits/stdc++.h>

using namespace std;

class a
{
public:
    int x;  
};

class a b
{
    // it fails if i add anything there
};

int main()
{
    return 0;
}

This fails to compile when I add something inside class a b. Could you please tell me what's going on?

Thank you!

Voicu
  • 56
  • 5

2 Answers2

8

WXDLLIMPEXP_FWD_CORE is a macro that is supposed to add a (compiler specific) class attribute and expands to __declspec(dllexport) or __declspec(dllimport) depending if used to export or import the class.

You example class a b { /* ... */ }; is simply invalid syntax,

Class names cannot contain whitespaces.

VZ.
  • 21,740
  • 3
  • 39
  • 42
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

The accepted answer is right regarding the macro definition, but also misleading regarding the "invalid syntax".

class a b { /* ... */ }; actually is valid syntax if your compiler supports uniform initialization. In the code you present it causes a variable of type a named b to be constructed in the global namespace using the default constructor. The class keyword works like typename in this case and the code should compile even without it.

It stops compiling when you try to define a variable inside the brackets since that's where uniform initialization syntax expects constructor call arguments to be placed.

Community
  • 1
  • 1
Vennor
  • 596
  • 6
  • 13