#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    10
    Rep Power
    0

    Reference to variables inside an object


    Is possible to have a variable inside an object that is a reference to a variable outside the object?

    How could I do that?

    Thanks in advance.
  2. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243
    I am not sure what you mean. You can have pointers in objects that point to other objects and in C (and C++ if you force it) you can have void pointers that point at anything, anywhere.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    10
    Rep Power
    0
    In a function you can do this:

    Code:
    int x = 1;
    int &y = x;
    
    y = 100;
    
    cout << x;
    And that whould result in 100 because y is a reference to x. I wat to have something like that but in an object to a variable outside the object.
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    243

    Is this what you are talking about?


    Code:
    #include <iostream>
    
    class first {
    public:
        int intValInFirst;
    };
    
    class second {
    public:
        first *myFirst;
    };
    
    int main(){
        first myOuterFirst;
        second mySecond;
    
        myOuterFirst.intValInFirst = 10;
    
        std::cout << "myOuterFirst.intValInFirst: " << myOuterFirst.intValInFirst << std::endl;
    
        mySecond.myFirst = &myOuterFirst;
    
        std::cout << "mySecond.myFirst->intValInFirst: " << mySecond.myFirst->intValInFirst << std::endl;
    
        mySecond.myFirst->intValInFirst = 20;
    
        std::cout << "\n\nChange value\n\n";
    
        std::cout << "mySecond.myFirst->intValInFirst: " << mySecond.myFirst->intValInFirst << std::endl;
        std::cout << "myOuterFirst.intValInFirst: " << myOuterFirst.intValInFirst << std::endl;
    
        return 0;
    }
    Output:

    Code:
    myOuterFirst.intValInFirst: 10
    mySecond.myFirst->intValInFirst: 10
    
    
    Change value
    
    mySecond.myFirst->intValInFirst: 20
    myOuterFirst.intValInFirst: 20

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo