Thread: class definition syntax question

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    596

    class definition syntax question

    I found this format in some, but not all, of the class heads in the svn source code for the PLplot plotting library (specifically, in headers and source files in the bindings/c++ and bindings/wxwidgets directories).

    Code:
    class PLDLLIMPEXP_CXX plstream {
    
    // ...
    };
    I don't recall ever having seen anything between the keyword "class" and the classname. What is that term "PLDLLIMPEXP_CXX" and what does it do?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is a macro. Something like this can be found by googling:

    Code:
    > #ifdef USINGDLL
    >   #if defined(WIN32)
    >     /* Visual C/C++, Borland, MinGW and Watcom */
    >     #if defined(__VISUALC__) || defined(_MSC_VER) ||
    > defined(__BORLANDC__) || defined(__GNUC__) || defined(__WATCOMC__)
    >       #define PLDLLEXPORT __declspec(dllexport)
    >       #define PLDLLIMPORT __declspec(dllimport)
    >     #else
    >       #define PLDLLEXPORT
    >       #define PLDLLIMPORT
    >     #endif
    >   #elif defined(__CYGWIN__)
    >     #define PLDLLEXPORT __declspec(dllexport)
    >     #define PLDLLIMPORT __declspec(dllimport)
    >   #elif defined(__GNUC__) && __GNUC__ > 3
    >     /* Follow ideas in http://gcc.gnu.org/wiki/Visibility for GCC
    > version 4.x
    >      * The following forces exported symbols specifically designated  
    > with
    >      * PLDLLEXPORT to be visible.  */
    >     #define PLDLLEXPORT __attribute__ ((visibility("default")))
    >     #define PLDLLIMPORT
    >   #endif
    > #endif
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    How and why would a macro be expanded in the middle of a class head? I've only seen
    Code:
    class [classname] { ... };
    I've never seen
    Code:
    class [blah, blah, blah] [classname] { ... };
    What sort of "blah, blah, blah" can go in there?

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    OK, I can answer the "what", but not the "how" or the "why". I found various definitions in another file, include/pldll.h. Depending on the compiler, the resulting code may be any of these:
    Code:
    class _declspec( dllexport ) plstream { //...
    class _declspec( dllimport ) plstream { //...
    class __attribute__ (( visibility( "default" ))) plstream { //...
    // and so on...
    but I'm still left with my original question. What are these terms that are placed between "class" and "classname", and (in general) what purpose do they serve?

    Or, to put it another way, what should I google to find out more about this?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    They export or import classes from or in a dll, basically.
    You should be able to find easy reference of __declspec(dllexport) (and import respectively). The only way they differ from functions is that they export all the functions and variables inside the class so the class can be used in an application from the DLL.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    These are compiler-specific ways to compile that class into a DLL, or something.

    dllexport, dllimport (C++)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Hmm. I'm familiar enough with using libraries, but not clear on the meaning of importing and exporting wrt libraries, so I guess I'll have to read up on how libraries work under the hood. Thanks.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The basic idea is that the code is stored inside a file of its own.
    Multiple processes can load this and use this shared code. Moreover, a dynamic linked library is shared between processes, so the dll is loaded into memory only once. So multiple processes can use the code without having the code stored in their executables.

    But processes can only access what you "export" from the dll. And to use it, the processes need to "import" it from the dll. That's the basic story.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Quote Originally Posted by Elysia View Post
    The basic idea is that the code is stored inside a file of its own.
    Multiple processes can load this and use this shared code. Moreover, a dynamic linked library is shared between processes, so the dll is loaded into memory only once. So multiple processes can use the code without having the code stored in their executables.

    But processes can only access what you "export" from the dll. And to use it, the processes need to "import" it from the dll. That's the basic story.
    That's helpful. Thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  5. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM