Necip
Necip

Reputation: 341

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?

Upvotes: 2

Views: 520

Answers (2)

Idan K
Idan K

Reputation: 20891

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.

Upvotes: 4

xtofl
xtofl

Reputation: 41519

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.

Upvotes: 1

Related Questions