initialize static structured variable challenge

Discussion in 'C++' started by wim delvaux, Jun 23, 2004.

  1. wim delvaux

    wim delvaux Guest

    Hi all,

    I want to initialize two structures with pointers
    to eachother

    static StructA VarA = { PtrToVarB };
    static StructB VarB = { PtrToVarA };

    A solution that WORKS in C (using gcc 3.3) is

    static StructA VarA; // defining VarA
    static StructB VarB; // defining VarB

    and then

    static StructA VarA = { &VarB };
    static StructB VarB = { &VarA };

    However in C++ (same gcc version) it does not work because
    the first static is not assumed to be tentative.

    How can I solve this problem ? Initializing using
    functions is not possible since the above code is
    generated automatically by some compiler

    W
     
    wim delvaux, Jun 23, 2004
    #1
    1. Advertising

  2. wim delvaux wrote:
    > Hi all,
    >
    > I want to initialize two structures with pointers
    > to eachother
    >
    > static StructA VarA = { PtrToVarB };
    > static StructB VarB = { PtrToVarA };
    >
    > A solution that WORKS in C (using gcc 3.3) is
    >
    > static StructA VarA; // defining VarA
    > static StructB VarB; // defining VarB
    >
    > and then
    >
    > static StructA VarA = { &VarB };
    > static StructB VarB = { &VarA };
    >
    > However in C++ (same gcc version) it does not work because
    > the first static is not assumed to be tentative.
    >
    > How can I solve this problem ? Initializing using
    > functions is not possible since the above code is
    > generated automatically by some compiler


    namespace
    {
    extern StructA VarA;
    extern StructB VarB;

    StructA VarA = { &VarB };
    StructB VarB = { &VarA };
    };

    I didn't test it, but the above should compile. Note that the "extern"
    in an anonymous namespace has about the same meaning as static.
     
    Gianni Mariani, Jun 23, 2004
    #2
    1. Advertising

  3. wim delvaux

    wim delvaux Guest

    namespace {
    typedef struct x_s {
    int x;
    } x_t;

    extern x_t X1;
    extern x_t X2;

    static x_t X1 = { &X2 };
    static x_t X2 = { &X1 };
    }

    Compiled above code ...

    u19809@buro:.../tmp$ c++ --version
    c++ (GCC) 3.3.3 (Debian 20040417)
    Copyright (C) 2003 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Result :

    u19809@buro:.../tmp$ c++ t.c -o t++
    t.c:9: error: `<unnamed>::X1' was declared `extern' and later `static'
    t.c:6: error: previous declaration of `<unnamed>::X1'
    t.c:9: error: invalid conversion from `<unnamed>::x_t*' to `int'
    t.c:10: error: `<unnamed>::X2' was declared `extern' and later `static'
    t.c:7: error: previous declaration of `<unnamed>::X2'
    t.c:10: error: invalid conversion from `<unnamed>::x_t*' to `int'
     
    wim delvaux, Jun 23, 2004
    #3
  4. wim delvaux

    Buster Guest

    wim delvaux wrote:

    What you tried is totally different from what Gianni suggested.
    The 'static' keyword has managed to sneak back in. I compiled
    the following, as a complete translation unit. There were no
    errors on GCC 3.3.1, MSVC 7.1 or BCB 6.0.

    namespace
    {
    struct StructB;
    struct StructA { StructB * p; };
    struct StructB { StructA * p; };

    extern StructA VarA;
    extern StructB VarB;

    StructA VarA = { &VarB };
    StructB VarB = { &VarA };
    };

    void f()
    {
    StructA a;
    StructB b;
    }

    --
    Regards,
    Buster.
     
    Buster, Jun 23, 2004
    #4
  5. wim delvaux

    wim delvaux Guest

    Ah ok,

    one thing I am afraid of is that VarA and VarB should remain local
    variables (i.e. not cause nameclashes in other modules).

    Does the use of the anonymous namespace imply that the vars are hidden for
    all other source files ?

    W
     
    wim delvaux, Jun 23, 2004
    #5
  6. wim delvaux wrote:
    > Ah ok,
    >
    > one thing I am afraid of is that VarA and VarB should remain local
    > variables (i.e. not cause nameclashes in other modules).
    >
    > Does the use of the anonymous namespace imply that the vars are hidden for
    > all other source files ?


    yes - (modulo compiler bugs).
     
    Gianni Mariani, Jun 23, 2004
    #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. J. Bayjan
    Replies:
    1
    Views:
    1,272
    Jim Sculley
    Jan 27, 2004
  2. lou zion
    Replies:
    8
    Views:
    6,034
    Howard
    Jun 9, 2005
  3. Scott Abel
    Replies:
    0
    Views:
    443
    Scott Abel
    Jul 5, 2006
  4. Nan Li
    Replies:
    7
    Views:
    552
    Nan Li
    Nov 9, 2005
  5. markww
    Replies:
    6
    Views:
    5,151
    Salt_Peter
    Aug 26, 2006
Loading...

Share This Page