Globals from a namespace

Discussion in 'C++' started by jgraveskc@gmail.com, Mar 6, 2007.

  1. Guest

    I know this sounds nasty, but let me explain.
    I have what was a class that contains a whole buncha function pointers
    (for OpenGL extensions) that are used in the app. I was porting this
    app to macs, where the extensions are properly present (just normal
    functions) so the class is not needed. I was wanting a way to make
    the same code work on both. For example, if I could do this:
    glGenBuffersARB(bla); on both. On the mac its just accessing the
    OpenGL function, on the Windows machine it would have a using
    namespace whatever; at the top and is actually accessing those
    function pointers of the same name.

    This seemed like it would work fine for my purposes, but I can't seem
    to get the namespace pointers to be truly shared across the app. It
    seems like every function that wants to use them has to assign them
    itself.

    Right now it looks like this (this isn't a copy paste [hopefully
    obviously] just an example):
    namespace whatever
    {
    static PtrType func1;
    static PtrType2 func2;
    // etc

    InitPointers(); // defined in cpp; assignes all the function pointers
    with wglGetProcAddress
    };


    Any ideas to make this work without having to do a whole lot of #ifdef
    WIN32 #else's in the code?
    , Mar 6, 2007
    #1
    1. Advertising

  2. wrote:
    > I know this sounds nasty, but let me explain.
    > I have what was a class that contains a whole buncha function pointers
    > (for OpenGL extensions) that are used in the app. I was porting this
    > app to macs, where the extensions are properly present (just normal
    > functions) so the class is not needed. I was wanting a way to make
    > the same code work on both. For example, if I could do this:
    > glGenBuffersARB(bla); on both. On the mac its just accessing the
    > OpenGL function, on the Windows machine it would have a using
    > namespace whatever; at the top and is actually accessing those
    > function pointers of the same name.
    >
    > This seemed like it would work fine for my purposes, but I can't seem
    > to get the namespace pointers to be truly shared across the app. It
    > seems like every function that wants to use them has to assign them
    > itself.
    >
    > Right now it looks like this (this isn't a copy paste [hopefully
    > obviously] just an example):
    > namespace whatever
    > {
    > static PtrType func1;
    > static PtrType2 func2;
    > // etc


    Why 'static'? What if you drop 'static'? If you have this in the
    header, you need to add 'extern'. Then in one of the C++ modules
    you need to actually initialise them with something (NULL should be
    fine), and then make 'InitPointers()' be called from another object's
    initialisation:

    int dummy = (InitPointer(), 42);

    >
    > InitPointers(); // defined in cpp; assignes all the function pointers
    > with wglGetProcAddress
    > };
    >
    >
    > Any ideas to make this work without having to do a whole lot of #ifdef
    > WIN32 #else's in the code?


    See above.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Mar 6, 2007
    #2
    1. Advertising

  3. Rolf Magnus Guest

    wrote:

    > I know this sounds nasty, but let me explain.
    > I have what was a class that contains a whole buncha function pointers
    > (for OpenGL extensions) that are used in the app. I was porting this
    > app to macs, where the extensions are properly present (just normal
    > functions) so the class is not needed. I was wanting a way to make
    > the same code work on both. For example, if I could do this:
    > glGenBuffersARB(bla); on both. On the mac its just accessing the
    > OpenGL function, on the Windows machine it would have a using
    > namespace whatever; at the top and is actually accessing those
    > function pointers of the same name.
    >
    > This seemed like it would work fine for my purposes, but I can't seem
    > to get the namespace pointers to be truly shared across the app. It
    > seems like every function that wants to use them has to assign them
    > itself.
    >
    > Right now it looks like this (this isn't a copy paste [hopefully
    > obviously] just an example):
    > namespace whatever
    > {
    > static PtrType func1;
    > static PtrType2 func2;


    Why are those static? That means every translation unit that wants to use
    them needs its own set of pointers. Maybe that's where your problem is
    coming from?

    > // etc
    >
    > InitPointers(); // defined in cpp; assignes all the function pointers
    > with wglGetProcAddress
    > };
    >
    >
    > Any ideas to make this work without having to do a whole lot of #ifdef
    > WIN32 #else's in the code?
    Rolf Magnus, Mar 6, 2007
    #3
  4. Rolf Magnus Guest

    Victor Bazarov wrote:

    > wrote:
    >> I know this sounds nasty, but let me explain.
    >> I have what was a class that contains a whole buncha function pointers
    >> (for OpenGL extensions) that are used in the app. I was porting this
    >> app to macs, where the extensions are properly present (just normal
    >> functions) so the class is not needed. I was wanting a way to make
    >> the same code work on both. For example, if I could do this:
    >> glGenBuffersARB(bla); on both. On the mac its just accessing the
    >> OpenGL function, on the Windows machine it would have a using
    >> namespace whatever; at the top and is actually accessing those
    >> function pointers of the same name.
    >>
    >> This seemed like it would work fine for my purposes, but I can't seem
    >> to get the namespace pointers to be truly shared across the app. It
    >> seems like every function that wants to
    >>
    >> Right now it looks like this (this isn't a copy paste [hopefully
    >> obviously] just an example):
    >> namespace whatever
    >> {
    >> static PtrType func1;
    >> static PtrType2 func2;
    >> // etc

    >
    > Why 'static'? What if you drop 'static'? If you have this in the
    > header, you need to add 'extern'. Then in one of the C++ modules
    > you need to actually initialise them with something (NULL should be
    > fine),


    Globals are automatically initialized (to zero in case of PODs).

    > and then make 'InitPointers()' be called from another object's
    > initialisation:
    >
    > int dummy = (InitPointer(), 42);


    I'm not sure that's possible in this case. The function might itself need
    some initialization of the OpenGL before, and I'm not sure if that is safe
    before main().
    Rolf Magnus, Mar 6, 2007
    #4
  5. >>
    >>Right now it looks like this (this isn't a copy paste [hopefully
    >>obviously] just an example):
    >>namespace whatever
    >>{
    >>static PtrType func1;
    >>static PtrType2 func2;

    >
    >
    > Why are those static? That means every translation unit that wants to use
    > them needs its own set of pointers. Maybe that's where your problem is
    > coming from?
    >


    I would guess because originally 'whatever' was a class, which at some
    point got changed to a namespace.

    john
    John Harrison, Mar 6, 2007
    #5
  6. Guest

    extern fixed it, thank you! I was unaware of this (probably
    obviously).
    By the way, I did require explicitly initializing them in a CPP file
    to prevent linker errors in response to those mentioning it.

    Thanks a lot guys!

    - Justin

    Victor Bazarov wrote:
    > wrote:
    > > I know this sounds nasty, but let me explain.
    > > I have what was a class that contains a whole buncha function pointers
    > > (for OpenGL extensions) that are used in the app. I was porting this
    > > app to macs, where the extensions are properly present (just normal
    > > functions) so the class is not needed. I was wanting a way to make
    > > the same code work on both. For example, if I could do this:
    > > glGenBuffersARB(bla); on both. On the mac its just accessing the
    > > OpenGL function, on the Windows machine it would have a using
    > > namespace whatever; at the top and is actually accessing those
    > > function pointers of the same name.
    > >
    > > This seemed like it would work fine for my purposes, but I can't seem
    > > to get the namespace pointers to be truly shared across the app. It
    > > seems like every function that wants to use them has to assign them
    > > itself.
    > >
    > > Right now it looks like this (this isn't a copy paste [hopefully
    > > obviously] just an example):
    > > namespace whatever
    > > {
    > > static PtrType func1;
    > > static PtrType2 func2;
    > > // etc

    >
    > Why 'static'? What if you drop 'static'? If you have this in the
    > header, you need to add 'extern'. Then in one of the C++ modules
    > you need to actually initialise them with something (NULL should be
    > fine), and then make 'InitPointers()' be called from another object's
    > initialisation:
    >
    > int dummy = (InitPointer(), 42);
    >
    > >
    > > InitPointers(); // defined in cpp; assignes all the function pointers
    > > with wglGetProcAddress
    > > };
    > >
    > >
    > > Any ideas to make this work without having to do a whole lot of #ifdef
    > > WIN32 #else's in the code?

    >
    > See above.
    >
    > V
    , Mar 7, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Èý¹â
    Replies:
    1
    Views:
    567
    William F. Robertson, Jr.
    Jul 29, 2003
  2. Replies:
    0
    Views:
    5,129
  3. Anonymous
    Replies:
    3
    Views:
    531
    Ron Natalie
    Aug 18, 2003
  4. Jacek Generowicz
    Replies:
    1
    Views:
    390
    Daniel Dittmar
    Apr 8, 2004
  5. Steven W. Orr
    Replies:
    6
    Views:
    274
    Steve Holden
    Apr 28, 2007
Loading...

Share This Page