tags:

views:

154

answers:

2

Hi Everyone,

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?

+1  A: 

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.

xtofl
+4  A: 

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.

Idan K
We had to use second approach once. Reason was, that exported class was derived from template class and something failed (VC 2005)
Konstantin
Thanks for the answer it has helped me quite a lot
Necip