f



Qualifying class declarations with defined symbols in C++

Hi!

I am looking into some C++ code written by others. They declare some
classes like the following:

---
class SLICE_API BoolTok : public GrammarBase {...};
---

I wander what the meaning of the SLICE_API qualifier is in the previous
declaration? I have never seen anyone qualify the new class-name like that
before.

By poking around in the rest of the code, I have found SLICE_API to be
declared in an included file, as follows:

---
#ifndef SLICE_API
#   ifdef SLICE_API_EXPORTS
#       define SLICE_API ICE_DECLSPEC_EXPORT
#   else
#       define SLICE_API ICE_DECLSPEC_IMPORT
#   endif
#endif
---

What do they do here, and what bearing does it have on the class
declaration above?

Thanks if you can help.
Catalin


-- 

	<<<< ================================== >>>>
	<<     We are what we repeatedly do.      >>
	<<  Excellence, therefore, is not an act  >>
	<<             but a habit.               >>
	<<<< ================================== >>>>
0
Doru
11/9/2003 5:25:41 PM
comp.os.linux.development.apps 5216 articles. 1 followers. Post Follow

9 Replies
181 Views

Similar Articles

[PageSpeed] 44

Doru-Catalin Togea wrote:
> class SLICE_API BoolTok : public GrammarBase {...};
> 
> I wander what the meaning of the SLICE_API qualifier is in the previous
> declaration? 

> #ifndef SLICE_API
> #   ifdef SLICE_API_EXPORTS
> #       define SLICE_API ICE_DECLSPEC_EXPORT
> #   else
> #       define SLICE_API ICE_DECLSPEC_IMPORT
> #   endif
> #endif

You should have looked further. ICE_DECLSPEC_* resolve to an empty string
on your system. On win32, this is used to tell the compiler that a symbol
is imported or exported from a DLL, depending on whether you are building
or using the library. They then resolve to either __declspec(dllimport) or
__declspec(dllimport).

Uli

-- 
Questions ?
see  C++-FAQ Lite: http://parashift.com/c++-faq-lite/  first !

0
Ulrich
11/9/2003 9:26:19 PM
On Sun, 9 Nov 2003, Ulrich Eckhardt wrote:

> > class SLICE_API BoolTok : public GrammarBase {...};

> You should have looked further. ICE_DECLSPEC_* resolve to an empty string
> on your system. On win32, this is used to tell the compiler that a symbol
> is imported or exported from a DLL, depending on whether you are building
> or using the library. They then resolve to either __declspec(dllimport) or
> __declspec(dllimport).

OK, then again, what is the meaning of the SLICE_API qualifier in the
above class declaration? That is what is the difference between

	class SLICE_API BoolTok : public GrammarBase {...};

and

	class BoolTok : public GrammarBase {...};


Catalin

-- 

	<<<< ================================== >>>>
	<<     We are what we repeatedly do.      >>
	<<  Excellence, therefore, is not an act  >>
	<<             but a habit.               >>
	<<<< ================================== >>>>
0
Doru
11/9/2003 11:57:12 PM
Doru-Catalin Togea wrote:
> That is what is the difference between
> 
> 	class SLICE_API BoolTok : public GrammarBase {...};
> 
> and
> 
> 	class BoolTok : public GrammarBase {...};

On windows, you'd only be able to use the first one from outside the 
..dll that contains this piece of code.

0
Christian
11/10/2003 12:28:22 AM
Doru-Catalin Togea <doru-cat@ifi.uio.no> writes:

> OK, then again, what is the meaning of the SLICE_API qualifier in the
> above class declaration? That is what is the difference between
> 
> 	class SLICE_API BoolTok : public GrammarBase {...};
> 
> and
> 
> 	class BoolTok : public GrammarBase {...};

The answer repeats.

On a Linux system, none (there is *no* difference), since SLICE_API
is '#defined' to nothing.

On a Win32 system one must explicitly tell the linker which symbols
should be exported from a shared library (a .DLL). This is done either
by using .DEF files at link time; or by embellishing source with e.g.

  int __declspec(dllexport) foo; // tell linker to export definition of foo

Since BoolTok developers did not want to write 2 versions of
this class for Unix and Windows, they used a macro (SLICE_API)
and defined it appropriately for each system.

Cheers,
-- 
In order to understand recursion you must first understand recursion.
0
Paul
11/10/2003 7:05:01 AM
On Mon, 10 Nov 2003, Paul Pluzhnikov wrote:

> Doru-Catalin Togea <doru-cat@ifi.uio.no> writes:
>
> > OK, then again, what is the meaning of the SLICE_API qualifier in the
> > above class declaration? That is what is the difference between
> >
> > 	class SLICE_API BoolTok : public GrammarBase {...};
> >
> > and
> >
> > 	class BoolTok : public GrammarBase {...};
>
> The answer repeats.

Please bear with my ignorance. I understand your explanations, and I
didn't know everything you explained to me. So thanks. What still I do not
understand is the language construct used.

I understand HOW you can use defines in order to customize your code for
different OSs, etc., but I wonder why did they have to put the "SLICE_API"
qualifier before the class name?

One thing is to use "#ifdef"s to customize code, and another thing is to
use a #defined (can I say) constant in a class declaration.

Say in Win32, where SLICE_API would get defined to something:

1) What will "class SLICE_API BoolTok : public GrammarBase {...};" expand
to after preprocessing?

Now say SLICE_API gets the "whatever" value during preprocessing.
2) What will the "whatever" qualifier tell the compiler/linker to do about
this class definition: "class whatever BoolTok : public GrammarBase {...};"

I have never met such a language construct before. I have read some
books about C++, I have Stroustroup's book as well, but I can not remember
ever reading that you qualify a class name in a class declaration. They
don't use the "." qualifier operator either, like in

	class SLICE_API.BoolTok : public GrammarBase {...};

So again, sorry for my ignorance. Can I read the answers to my questions
in Stroustroup's book? If so it in the section dealing with what?

Thanks a lot,
Catalin

> On a Linux system, none (there is *no* difference), since SLICE_API
> is '#defined' to nothing.
>
> On a Win32 system one must explicitly tell the linker which symbols
> should be exported from a shared library (a .DLL). This is done either
> by using .DEF files at link time; or by embellishing source with e.g.
>
>   int __declspec(dllexport) foo; // tell linker to export definition of foo
>
> Since BoolTok developers did not want to write 2 versions of
> this class for Unix and Windows, they used a macro (SLICE_API)
> and defined it appropriately for each system.
>
> Cheers,
>

-- 

	<<<< ================================== >>>>
	<<     We are what we repeatedly do.      >>
	<<  Excellence, therefore, is not an act  >>
	<<             but a habit.               >>
	<<<< ================================== >>>>
0
Doru
11/10/2003 1:02:01 PM
Doru-Catalin Togea <doru-cat@ifi.uio.no> writes:

> 1) What will "class SLICE_API BoolTok : public GrammarBase {...};" expand
> to after preprocessing?

   class __declspec(dllexport) BoolTok : public ...

> 2) What will the "whatever" qualifier tell the compiler/linker to do about
> this class definition: "class whatever BoolTok : public GrammarBase {...};"

It will tell the linker to export all BoolTok member functions and
static variables (if any) from the DLL.

> I have never met such a language construct before. 

This is not a C++ language construct, and you are unlikely to find it
in a (non Win32-specific) C++ programming book.

This is a MicroSoft *extension* to the C++ language, designed to make
life of Win32-only programmers easier (at the expense of everyone else).

Cheers,
-- 
In order to understand recursion you must first understand recursion.
0
Paul
11/10/2003 4:05:01 PM
Paul Pluzhnikov wrote:
>    class __declspec(dllexport) BoolTok : public ...

Note that it will only look like this while the dll itself is compiled. 
When users of it are compiled, it will look like:

     class __declspec(dllimport) BoolTok : public ...

0
Christian
11/10/2003 11:44:34 PM
On Mon, 10 Nov 2003, Paul Pluzhnikov wrote:

> > I have never met such a language construct before.
>
> This is not a C++ language construct, and you are unlikely to find it
> in a (non Win32-specific) C++ programming book.
>
> This is a MicroSoft *extension* to the C++ language, designed to make
> life of Win32-only programmers easier (at the expense of everyone else).

Thanks, I am relived. I was wandering if I have missed some basic
understanding of the C++ syntax.

Catalin


-- 

	<<<< ================================== >>>>
	<<     We are what we repeatedly do.      >>
	<<  Excellence, therefore, is not an act  >>
	<<             but a habit.               >>
	<<<< ================================== >>>>
0
Doru
11/11/2003 8:56:25 AM
Im Artikel <ur80gk9vs.fsfYB8X@earthlink.net>, Paul Pluzhnikov
<ppluzhnikov@earthlink.net> schreibt:

>On a Linux system, none (there is *no* difference), since SLICE_API
>is '#defined' to nothing.

I have a related question, with "extern" declarations. In former times it was
not allowed to define variables which have been declared "extern" before, i.e.
in some header file. This lead to similar constructs, where EXTERN was #defined
to either "extern" or "" (nothing), for the compilation of the using or
declaring modules. But nowadays such constructs seem to be no more required?

DoDi
0
vbdis
11/14/2003 3:58:30 PM
Reply: