Question

I started programming in c, and then worked little bit on Java and c#.

  • So my understanding of an object(instance of a class) is that its like a pointer to the instance stored in memory(may be which points to the first memory cell of the whole instance...similar to struct or array A[] has its initial address stored in A).

Is my above understanding correct?

Reading from the above I understand both are same except references gaurds you by not taking null.

One point I don't get is they say: Pointer variable can be reassigned after initializing, but reference variable cannot be.But I tested this scenario and compiler allows me to re-assign, does it depend on the compiler??. If at all it cannot be reassigned a different value, what is the use of 'pass by ref' at all(in c++)?

Also can someone suggest a reliable and cool online source for c++, like msdn for c#

And also it would be nice if there is an easy way to see the address stored in a reference variable in c++

Was it helpful?

Solution

I believe your understanding is mostly correct.

Regarding your final question, in C++ you can change what a pointer points to by giving it a new value:

int a, b;
int *p = &a;
p = &b;

However, you can't do the same with references:

int a, b;
int &r = a;
// ??? no syntax to make r refer to b at this point

OTHER TIPS

If so why do c# has pass by reference

C# isn't pass by reference.. it is pass-by-value (unless explicitly using ref). The value of the reference is copied.

You can think of references as pointers.. with type safety. The underlying implementation detail is of little consequence (although, as I understand it, they currently are a wrapper around a pointer.. but they may not always be).

Pointer variable can be reassigned after initializing, but reference variable cannot be.But I tested this scenario and compiler allows me to re-assign,

There is no restriction on re-assigning a reference like you describe. A new reference will be copied over the top of the existing one. So I'm not sure I understand that issue.

I think your understanding of the above is skewed when passing references through methods. Here is an example:

public class Customer {
    public string FirstName { get; set; }
}

public static void Main() {
    var customer = new Customer() { FirstName = "Simon" };
    Example(customer);
    Console.WriteLine(customer.FirstName);
}

public static void Example(Customer customer) {
    customer = new Customer() { FirstName = "CHANGED" };
}

(Working example here)

What would this print? The answer is "Simon". The reference was copied into the function. You've re-assigned the local reference. When exiting.. the original reference remains unchanged.

How do you "fix" that? (Not that I would expect the below behaviour..) .. you pass by ref explicitly:

public class Customer {
    public string FirstName { get; set; }
}

public static void Main() {
    var customer = new Customer() { FirstName = "Simon" };
    Example(ref customer); // By ref
    Console.WriteLine(customer.FirstName);
}

public static void Example(ref Customer customer) { // By ref
    customer = new Customer() { FirstName = "CHANGED" };
} 

(Working sample here)

This prints "CHANGED", since the explicit pass-by-ref has been requested by the user of the ref keyword.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top