Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > extern const reference

Reply
Thread Tools

extern const reference

 
 
Igor R.
Guest
Posts: n/a
 
      11-22-2011
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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-22-2011
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


 
Reply With Quote
 
 
 
 
Jens Thoms Toerring
Guest
Posts: n/a
 
      11-22-2011
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
non-const reference and const reference George2 C++ 10 12-17-2007 02:19 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
extern const char * vs. extern const char []http://tinyurl.com/47e3k Thomas Matthews C++ 5 08-02-2004 10:36 AM



Advertisments