How to add constructors/destructors to an unnamed class?

后端 未结 3 963
广开言路
广开言路 2020-12-02 20:27

Is there a way to declare a constructor or a destructor in an unnamed class? Consider the following

void f()
{
    struct {
        // some implementation
          


        
3条回答
  •  春和景丽
    2020-12-02 20:52

    If you are thinking of C++ names, then any class that has objects has to have a destructor whether you create it explicitly or not. So yes, the compiler knows how to assign a name. Whether that naming convention is any of your business, however, probably not.

    Actually, you can create a structure or also a namespace without a name. You still need to have names somewhere because at the time you link all of that, the linker needs some kind of a name to make it all work, although in many cases it will be local names that are resolved immediately at compile time--by the assembler.

    One way to know of the names assigned by the compiler is to look at the debug strings and see what corresponds to the different addresses that you're interested in. When you compile with -g then you should get all the necessary debug for your debugger to place your current at the right place with the right "names"... (I've see the namespaces without a name it says " namespace", I'm pretty sure structures use the same trick at a higher level.)

提交回复
热议问题