2 Replies - 698 Views - Last Post: 05 November 2011 - 04:56 PM Rate Topic: -----

#1 dragstang86   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 28-September 09

If Using Reference Parameters...

Posted 05 November 2011 - 01:09 PM

I have dominantly studied Java and am now starting to explore C++ and specifically reference parameters and for the most part I understand how it works but I am looking for clarification in regards to how C++ handles local variables overriding reference parameters. The following better shows what I am referring to.

Consider the following Java code:

class Int
{
    public int value;
}

class Parameters 
{
    public static void main(String [] args)
    {
        int value1 = 1;
        Int value2 = new Int(), value3 = new Int();
        value2.value = 1;
        value3.value = 1;
        increment(value1, value2, value3);
        System.out.println( "value1 = " + value1 +
            " value2 = " + value2.value + " value3 = " + value3.value);
    }

    private static void increment(int value1, Int value2, Int value3)
    {
        value1++;
        value2.value++;
        int newValue = value3.value + 1;
        value3 = new Int();
        value3.value = newValue;
    }
}



I know this code in Java produces the following output:
value1 = 1 value2 = 2 value3 = 1

Now my question is: If this was a C++ program and all parameters were REFERENCE PARAMETERS, how would value3 be handled? I know the output for value1 and value2 would be:
value1 = 2 value2 = 2 value3 = ?

Is This A Good Question/Topic? 0
  • +

Replies To: If Using Reference Parameters...

#2 Oler1s   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: If Using Reference Parameters...

Posted 05 November 2011 - 01:33 PM

There isn't a direct parallel in C++ to the Java code you wrote. I don't think you've understood the underlying concepts.

In Java,

Int value2 = new Int()



Involves two concepts. One is the idea of instantiating an object of class Int. That's what new Int() does. The other is the reference value2. In other words, there's a difference between the object and the reference to that object. value2 is not the object. It's a reference to an object.

In C++, this concept does not exist at all.

class Int
{
	int value;
}

int main()
{
	Int value2;
}



In C++, value2 is not a reference. It is the object itself. Consequently, you cannot make it another object, because that's nonsensical. There is no direct parallel to this in C++: value3 = new Int(); .

You'll typically see people mimic the above with pointers, but that doesn't have anything to do with references.

This post has been edited by Oler1s: 05 November 2011 - 01:34 PM

Was This Post Helpful? 1
  • +
  • -

#3 dragstang86   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 28-September 09

Re: If Using Reference Parameters...

Posted 05 November 2011 - 04:56 PM

Ahh brain fart. I had C++ on the mind for some reason but I meant to say C#. I'll post it in the correct forum. I need more sleep! Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1