Thread: extern classes

  1. #1
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38

    extern classes

    Hi all,

    I have a quick question on the usage of the extern keyword. I'm trying to get a variable to be of global scope, but as it is of a custom type, I'm not sure how to use extern and Google doesn't seem to help me.

    I'll provide the following code sample to try and illustrate the point better:

    Code:
    //In header file included at entry point
    
    namespace custom
    {
    
    int myInt;
    char myChar;
    ...
    //global variable here
    Class4 myVar;
    
    };
    
    #include "class1.h"
    #include "class2.h" //calls myInt etc
    #include "class3.h"
    #include "class4.h"
    #include "class5.h"
    I've currently been thinking that I could open the namespace after the definition of class 4, but this seems a bit hacky. So really, this is a placement problem for which I thought of extern. Unfortunately, I can't extern myVar as it defaults to type int, and I'm not sure how to extern a class. I've tried this to no avail also:

    extern Class4;
    extern Class4 myVar;

    I was thinking that if I can extern to an atomic type, maybe I just needed to 'tell' the compiler that Class4 was a type.

    Is extern the right keyword to use? I know that this code is a bit messy, but I'm hoping that somebody may understand the problem I'm having.

    Thanks for your help,
    SM

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    extern Class4 myVar;
    is correct syntax.

    However, I think what you are struggling with is forward declaration of classes. You are including Class4.h after you use the class. You need to do two things:
    1. declare that there is a class called Class4 (forward declaration):
    Code:
    class Class4;
    2. Not instantiate the variable until you have declared the contents - the compiler does not know, until you have declared the full class, what the content of Class4 is, or how much space it will consume. So you have to make sure that the compiler knows what's inside Class4 before you make a variable of that type. That's just the rules, I'm afraid. If you MUST have "circular references" - such that an instance of Class4 must be known before you can declare the content of Class4, then you will need to use a pointer to the instance, so that the instance itself can be made later. (Pointers are always the same size, no matter what the content is [1])

    [1]Yes, pedants would point out that some architectures will have more complex rules about pointer size - but for the purposes of this discussion, I will ignore this part, as that's something the compiler can work out for itself in relevant cases.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern const?Please help
    By huwan in forum C++ Programming
    Replies: 10
    Last Post: 08-12-2008, 04:53 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Odd Problem with Linking
    By jamez05 in forum C++ Programming
    Replies: 6
    Last Post: 09-21-2005, 01:49 PM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 03:14 PM