Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > variable reuse

Reply
Thread Tools

variable reuse

 
 
Robin
Guest
Posts: n/a
 
      05-17-2010
Suppose I define a variable int a; and I want to be able to use the
same name a for a string variable or an int variable or any sort of
variable later on...how can I undefine it so I can do this?

Thanks in advance,
-Robin
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-17-2010
On 5/17/2010 1:09 PM, Robin wrote:
> Suppose I define a variable int a; and I want to be able to use the
> same name a for a string variable or an int variable or any sort of
> variable later on...how can I undefine it so I can do this?


You cannot undefine a variable. The name (and the definition) persists
to the end of the scope, which means you need to define your variables
each in its own scope, if you would like to reuse the name.

That said, two thoughts come to mind. First, don't name your variables
'a'. It's non-informative. It's better if the variable name is
self-documenting. Second, if you think you could reuse the name, it
sounds that either the name is too generic, or your algorithm should
probably be split into two portions and each wrapped in a function.

What is your intention in reusing the name? Obfuscation? Hope not.
Are you trying to optimize something? If yes, what? Also, it is quite
useful to post your code (even with parts removed) to illustrate your
points.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Robin
Guest
Posts: n/a
 
      05-17-2010
Thanks...
I will post my code next time, apologies...
-Robin

Victor Bazarov wrote:
> On 5/17/2010 1:09 PM, Robin wrote:
> > Suppose I define a variable int a; and I want to be able to use the
> > same name a for a string variable or an int variable or any sort of
> > variable later on...how can I undefine it so I can do this?

>
> You cannot undefine a variable. The name (and the definition) persists
> to the end of the scope, which means you need to define your variables
> each in its own scope, if you would like to reuse the name.
>
> That said, two thoughts come to mind. First, don't name your variables
> 'a'. It's non-informative. It's better if the variable name is
> self-documenting. Second, if you think you could reuse the name, it
> sounds that either the name is too generic, or your algorithm should
> probably be split into two portions and each wrapped in a function.
>
> What is your intention in reusing the name? Obfuscation? Hope not.
> Are you trying to optimize something? If yes, what? Also, it is quite
> useful to post your code (even with parts removed) to illustrate your
> points.
>
> V
> --
> I do not respond to top-posted replies, please don't ask

 
Reply With Quote
 
John H.
Guest
Posts: n/a
 
      05-17-2010
Victor Bazarov wrote:
> On 5/17/2010 1:09 PM, Robin wrote:
> > Suppose I define a variable int a; and I want to be able to use the
> > same name a for a string variable or an int variable or any sort of
> > variable later on...how can I undefine it so I can do this?

>
> The name (and the definition) persists
> to the end of the scope, which means you need to define your variables
> each in its own scope, if you would like to reuse the name.


Example:

int main()
{
int a = 97;

{ // Start a new block to begin a new scope
char a = 'a'; // now we can declare another "a" variable
}

{
char a[] = "a";
}

// The following would produce a compilation
// error because it would try to declare two a
// variables in the same scope
// double a = 97.0;
}
 
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
To reuse or not to reuse jacob navia C Programming 19 12-18-2006 07:22 AM
code reuse and design reuse sailor.gu@gmail.com C Programming 16 02-12-2006 09:09 PM
Reuse paramter list and reuse connection tshad ASP .Net 5 05-17-2005 12:33 AM
To reuse or not to reuse.... Hylander Java 0 02-26-2004 12:00 AM
shall I reuse a variable/signal or not? walala VHDL 1 09-14-2003 07:46 PM



Advertisments