简体   繁体   中英

Difference between two DLL declarations

I have a simple but subtle question. Below you see two different declaration variants of the same class from a DLL header file.

Can anybody tell me the difference of this class declaration;

class __declspec(dllexport) Car {
public:
    Car();
    void drive(void); 
    typedef enum { None, Indented } Formatting;
}

from this one?

class Car {
public:
    __declspec(dllexport) Car();
    __declspec(dllexport) void drive(void); 
    __declspec(dllexport) typedef enum { None, Indented } Formatting;
}

In the first declaration, the class itself is gets __declspec(dllexport) , whereas in the latter case each class element is declared individually so.

Are they different or do they have anything in common?

A brief test using depends showed that the first example exports one additional symbol compared to the second (btw you don't export an enum, it's not legal). If I'm not wrong I believe it was the default assignment operator.

The first approach exports the entire class, the second one just the methods that are prefixed with the declspec (no surprise here I guess).

So I'd say that the proper way of exporting a class is obviously the first one, personally I haven't seen any exported class using the second approach.

Exporting a class is shorthand for exporting all it's public functions.

So the difference is in the fact that the __declspec for the Formatting enum is nonsensical.

Sometimes it's more appropriate to export only a limited set of the class' functionality; then the latter form is to be preferred. Note that in your case, the 'automatically' generated destructor will not be exported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM