Question about multiple objects with the same name

This is a discussion on Question about multiple objects with the same name within the C++ Programming forums, part of the General Programming Boards category; Lets say i have this code: Code: #include <iostream> using namespace std; class test { int a; public: test(int); int ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    84

    Question about multiple objects with the same name

    Lets say i have this code:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class test
    {
        int a;
        public:
        test(int);
        int r_a();
    };
    
    int test::r_a()
    {
        return a;
    }
    
    test::test(int x)
    {
        a=x;
    }
    
    int main()
    {
        int y=0;
        char c;
    
        test b(y);
        y++;
    
        switch(c=cin.get())
        {
            case '1': {
                test b(y);
                y++;
            }
            default: y++; break;
        }
    
        cout <<b.r_a();
    }
    My question is:
    is it possible to have more than 1 object with the same name??

    and if yes, how do we distinguish them and call them individualy?? (how do u make them unique??)

    If not then what happens when i press 1 and create another object with the same class and the same name??? is the current object replaced by that new one?? (for example the first object had y==0 and now as i created a new one that value is overwriten staying only 1 object with y==1),
    or does the code ignore the creation of the new object assuming that we already have an object with the same name???

    Sory if it is confuse, help pls.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,262
    Quote Originally Posted by shiroaisu
    is it possible to have more than 1 object with the same name??

    and if yes, how do we distinguish them and call them individualy?? (how do u make them unique??)
    In a way yes, in different scopes or different namespaces.

    Quote Originally Posted by shiroaisu
    If not then what happens when i press 1 and create another object with the same class and the same name??? is the current object replaced by that new one??
    The variable named b in the scope of the switch hides the variable named b at function scope.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    The variable named b in the scope of the switch hides the variable named b at function scope.
    Indeed it does and this kind of behavior should be avoided b/c if you get into the habit of doing this it can create some very nasty hard to find bugs later. You should really try to avoid any kind of name collisions. The only time it really becomes a problem is in large code lines where many people are working on the code. Thankfully you can use namespaces to avoid this kind of issue. The only time namespaces will not help is when a symbol has been defined in the global scope such as in windows.h. No amount of trickery can get past this. If you want to see an example of it then create a class and put a function called GetObject() in it and make sure you include windows.h. You will see quickly that GetObject() or GetObjectW() has been defined and your code will not compile even if it is in a class in a namespace.
    Last edited by VirtualAce; 06-06-2010 at 11:51 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,262
    Quote Originally Posted by Bubba
    The only time namespaces will not help is when a symbol has been defined in the global scope such as in windows.h. No amount of trickery can get past this.
    The problem arises when the name is a macro name, since macros do not obey the rules of scope. Otherwise, one can have a name in the global namespace be the same as a name in some other namespace.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    84
    The variable named b in the scope of the switch hides the variable named b at function scope.
    sory i didnt really got this, ur saying that the second object called "g" hides the first one?? and if yes what do u mean by hiding???

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,262
    Quote Originally Posted by shiroaisu
    sory i didnt really got this, ur saying that the second object called "g" hides the first one?? and if yes what do u mean by hiding???
    There is no variable named g, but you probably mean b, and if so, yes. Here, hiding means that the name b in the scope of the switch refers to a different object from the name b at function scope. Effectively, you are unable to use the name b to access that other object, because that variable named b is hidden.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    To demonstrate variable hiding to yourself, run the following function, or at least tell us what you think it will print:
    Code:
    int foobar()
    {
        int foo = 1;
        cout << foo;
        {
            int foo = 2;
            cout << foo;
            {
                int foo = 3;
                cout << foo;
            }
            cout << foo;
        }
        cout << foo;
    }
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    The problem arises when the name is a macro name, since macros do not obey the rules of scope. Otherwise, one can have a name in the global namespace be the same as a name in some other namespace.
    Very true. My example was a poor one.

    How about this one then:

    Code:
    class A 
    {
        public:
          A();
          virtual ~A();
       
       private:
         int m_myVariable;
    };
    
    class B : public A
    {
        public:
          B();
          virtual ~B();
    
        private:
          int m_myVariable;
    };

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,262
    Quote Originally Posted by Bubba
    Very true. My example was a poor one.

    How about this one then:
    As an example of name hiding, that's fine, though I like the example where the name of an overloaded member function hides the names of member function inherited from the base class. But if you mean it as an example of where namespaces do not help, then that is true, but rather a moot point since m_myVariable is private. If it were non-private, then one could qualify the name and so access A::m_myVariable, and thus effectively get the same benefit as the use of a namespace.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    84
    Ok, tks everyone i now understand and ty for the example iMalc

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Definition Problem
    By EASports in forum Linux Programming
    Replies: 2
    Last Post: 07-14-2009, 12:42 PM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. Replies: 4
    Last Post: 06-18-2005, 03:26 PM
  4. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 11:01 AM
  5. Multiple Inheritance Question
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2002, 06:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21