0

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++

Community
  • 1
  • 1
username
  • 117
  • 1
  • 9
  • The reference itself can't refer to a new variable. And C# has passing by reference (I assume you mean `ref`) because value types are passed by value. Java has these as well (the primitives), but does not let you create your own like C# does (albeit limited). C++ does passing by value by default. – chris May 06 '14 at 03:25
  • A great, up to date reference for C++ is http://cppreference.com. – Greg Hewgill May 06 '14 at 03:27
  • I find http://www.cplusplus.com/ on google a lot.. is it good enough – username May 06 '14 at 03:29
  • Greg Hewgill answered the question in his comments. Thanks to him again – username May 06 '14 at 03:52

2 Answers2

1

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
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

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.

Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138