variable reuse

Discussion in 'C++' started by Robin, May 17, 2010.

  1. Robin

    Robin Guest

    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
    Robin, May 17, 2010
    #1
    1. Advertising

  2. 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
    Victor Bazarov, May 17, 2010
    #2
    1. Advertising

  3. Robin

    Robin Guest

    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
    Robin, May 17, 2010
    #3
  4. Robin

    John H. Guest

    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;
    }
    John H., May 17, 2010
    #4
    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. walala
    Replies:
    1
    Views:
    480
  2. tshad
    Replies:
    5
    Views:
    525
    Steve C. Orr [MVP, MCSD]
    May 17, 2005
  3. Hylander

    To reuse or not to reuse....

    Hylander, Feb 26, 2004, in forum: Java
    Replies:
    0
    Views:
    414
    Hylander
    Feb 26, 2004
  4. code reuse and design reuse

    , Feb 7, 2006, in forum: C Programming
    Replies:
    16
    Views:
    1,016
    Malcolm
    Feb 12, 2006
  5. jacob navia

    To reuse or not to reuse

    jacob navia, Nov 5, 2006, in forum: C Programming
    Replies:
    19
    Views:
    520
    Dave Thompson
    Dec 18, 2006
Loading...

Share This Page