2 Variables, 1 Address?

This is a discussion on 2 Variables, 1 Address? within the C++ Programming forums, part of the General Programming Boards category; Hullo hullo I was wondering if this is okay to do: Code: void Function2(int &MainX) { int X; &X = ...

  1. #1
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361

    2 Variables, 1 Address?

    Hullo hullo

    I was wondering if this is okay to do:
    Code:
    void Function2(int &MainX)
    {
    	int X;
    	&X = MainX
    }
    
    void Function1(void)
    {
    	int X = 2;
    	Function2(X);
    }
    And then use X in both functions as one in the same. So if in Function2 I change X to 5, it will change the X in Function1 to 5 as well (and vice versa).

    Is that okay to do?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    It would if you did
    Code:
    void Function2(int &X)
    {
    	X = 5;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Hmm, I know I'm just complicating things, but is there a way to do this with three variables like my first example?

    The reason is that I'm using classes and I'm trying to set private variables within those classes able to alter the initial variables. I'll complicate the code a bit:
    Code:
    class MyClass
    {
    public:
    	MyClass(int &MainX, int &MainY, int &MainZ);
    	~MyClass(void);
    private:
    	int X, Y;
    	int *Z;
    };
    
    MyClass::MyClass(int &MainX, int &MainY, int &MainZ)
    {
    	&X = MainX;
    	&Y = MainY;
    	&Z = MainZ;
    	X = 5;
    	Y = 2;
    	Z = 7;
    }
    
    void Function1(void)
    {
    	int X, Y;
    	int *Z;
    	MyClass MyVariable(X, Y, *Z);
    }
    So, if Function1 runs first, would the final values of X, Y, and Z not equal 5, 2, and 7 respectively? (Z must remain a pointer)

    (Based on your response Salem, I'm assuming not But I'm not sure why...)

    Thanks

  4. #4
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    What are memory addresses stored as? I think I may have a solution to this...

    Of which type of variable are they? (Float, Double, etc.)

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    161
    Code:
    &X = MainX;
    Did you even try to compile what you've posted? That isn't even a valid expression: you can't change the address of variables.

    If you want to change those values, you can return a reference:
    Code:
    class myclass
    {
    public:
      int& get_x() { return x; }
    
    private:
      int x;
    };
    
    int main()
    {
      myclass m;
      int& x = m.get_x();
      x = 10; // m.x will now be 10
    }

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    It's a bad idea to return a reference from a function, because the memory is not available outside the function.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Originally posted by XSquared
    It's a bad idea to return a reference from a function, because the memory is not available outside the function.
    This is only true if the reference is to a variable local to that function. In this case, the reference is to a member variable that is valid as long as the object itself is valid. Of course, there could still be problems with it if the object goes out of scope, but in the example provided it is fine.

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I'd do something like this (when you say Z must remain a pointer, do you mean MyClass::Z or the Z inside Function1?):
    Code:
    class MyClass
    {
    public:
    	MyClass(int &MainX, int &MainY, int &MainZ);
    	~MyClass() { }
    private:
    	int& X;
    	int& Y;
    	int* Z;
    };
    
    MyClass::MyClass(int &MainX, int &MainY, int &MainZ) :
      X(MainX), Y(MainY), Z(&MainZ)
    {
    	X = 5;
    	Y = 2;
    	*Z = 7;
    }
    
    void Function1()
    {
    	int X, Y;
    	int *Z = new int;
    	MyClass MyVariable(X, Y, *Z);
    	delete Z;
    }
    So now your class has references to X and Y. (By the way, references must be initialized in the initialization list instead of inside the constructor, which is why I added that). You also have a pointer to Z. Whenever you change X, Y or *Z inside your class, the variables passed into them will get the new values.

    Of course, this is dangerous, since once the function ends and X, Y and Z are destroyed, any time MyClass tries to access them you will get undefined behavior and possibly a crash.

    Also, its hard to tell if you understand between '&' the address of operator and '&' meaning reference, and '*' the dereference operator and '*' meaning pointer. If you don't, you might want to read up on that before continuing with whatever you are trying to do.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Address of variables?
    By alex1067 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 10:47 PM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 10:07 PM
  4. HelpMePlease
    By Prncess100 in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2002, 01:02 PM
  5. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM

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