extern const reference

Discussion in 'C++' started by Igor R., Nov 22, 2011.

  1. Igor R.

    Igor R. Guest

    Hello,

    I've got the following code:

    int main()
    {
    const extern int &ref;
    {
    int var = 5;
    const int &ref = var;
    }
    int var = ref;
    }

    I.e., I'd like to define and initialize a const ref in a local scope,
    but want to *declare* and to be able to use it in the outer scope. I
    attempt to declare it as extern, but this doesn't link well, i.e.
    "const int &ref = var;" isn't interpreted as the above extern
    definition, but as another local variable.
    Is it possible to do the above trick somehow? (I know I can
    restructure the code as the last resort.)

    Thanks.
    Igor R., Nov 22, 2011
    #1
    1. Advertising

  2. On 22.11.2011 20:47, Igor R. wrote:
    > Hello,
    >
    > I've got the following code:
    >
    > int main()
    > {
    > const extern int&ref;
    > {
    > int var = 5;
    > const int&ref = var;
    > }
    > int var = ref;
    > }
    >
    > I.e., I'd like to define and initialize a const ref in a local scope,
    > but want to *declare* and to be able to use it in the outer scope. I
    > attempt to declare it as extern, but this doesn't link well, i.e.
    > "const int&ref = var;" isn't interpreted as the above extern
    > definition, but as another local variable.
    > Is it possible to do the above trick somehow? (I know I can
    > restructure the code as the last resort.)


    For your given example you can just do

    int main()
    {
    int const ref = 5;
    int var = ref;
    }

    If that doesn't solve your problem, then it may be because I failed to
    understand what the original problem was.

    This sounds suspiciously like a case of trying to solve problem X by
    applying technique Y, then failing to make Y work, and asking about Y.
    Don't ask about technique Y. Ask about the original problem X.

    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Nov 22, 2011
    #2
    1. Advertising

  3. Igor R. <> wrote:
    > I've got the following code:


    > int main()
    > {
    > const extern int &ref;
    > {
    > int var = 5;
    > const int &ref = var;
    > }
    > int var = ref;
    > }


    > I.e., I'd like to define and initialize a const ref in a local scope,
    > but want to *declare* and to be able to use it in the outer scope. I
    > attempt to declare it as extern, but this doesn't link well, i.e.
    > "const int &ref = var;" isn't interpreted as the above extern
    > definition, but as another local variable.
    > Is it possible to do the above trick somehow? (I know I can
    > restructure the code as the last resort.)


    If you could do that in the sense you seem to want it to
    work the last line would involve undefined behaviour since
    you then would be using use a reference to a variable that
    already has gone out of scope. What you seem to try to get
    to work looks very similar to me to e.g.

    int const & get_var( )
    {
    int v = 5;
    return v;
    }

    int main( )
    {
    int const & ref = get_var( );
    int var = ref;
    }

    And that, of course, doesn't work since the moment the
    get_var() function is left nothing remains of its local
    variable 'v', so holding on to a reference to it doesn't
    make sense.

    What is what you're trying to do meant to be good for? Per-
    haps there's something that can be done but it's not really
    clear to me what you want to achieve.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
    Jens Thoms Toerring, Nov 22, 2011
    #3
    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. Thomas Matthews
    Replies:
    5
    Views:
    2,409
    tom_usenet
    Aug 2, 2004
  2. Replies:
    11
    Views:
    1,100
  3. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  4. George2
    Replies:
    10
    Views:
    594
    Pete Becker
    Dec 17, 2007
  5. Andre
    Replies:
    5
    Views:
    535
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page