Questions tagged [reference]

A reference is a value that enables a program to indirectly access a particular datum, such as a variable or a record, in the computer's memory or in some other storage device.

A reference is a value that enables a program to indirectly access a particular datum, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the datum.

In some programming languages, copying a variable may be implemented as either a copy by value or a copy by reference. When it is copied by reference, a reference to the data will be assigned, and the underlying data is not copied. Often, object datatypes will be copied by reference by default (for example, in Java), while primitives will be copied by value.

A nice article on pass by reference

17086 questions
215
votes
4 answers

What is the result type of '?:' (ternary/conditional operator)?

Why does the first conditional operator result in a reference? int x = 1; int y = 2; (x > y ? x : y) = 100; However, the second does not. int x = 1; long y = 2; (x > y ? x : y) = 100; Actually, the second does not compile at all: error: lvalue…
Yola
  • 18,496
  • 11
  • 65
  • 106
212
votes
7 answers

Java: difference between strong/soft/weak/phantom reference

I have read this article about different types of references in Java (strong, soft, weak, phantom), but I don't really understand it. What is the difference between these reference types, and when would each type be used?
user1204873
205
votes
11 answers

python list by value not by reference

Let's take an example a=['help', 'copyright', 'credits', 'license'] b=a b.append('XYZ') b ['help', 'copyright', 'credits', 'license', 'XYZ'] a ['help', 'copyright', 'credits', 'license', 'XYZ'] I wanted to append value in list 'b' but the value of…
d.putto
  • 7,185
  • 11
  • 39
  • 45
204
votes
3 answers

Why is 'this' a pointer and not a reference?

I was reading the answers to this question C++ pros and cons and got this doubt while reading the comments. programmers frequently find it confusing that "this" is a pointer but not a reference. another confusion is why "hello" is not of type…
Naveen
  • 74,600
  • 47
  • 176
  • 233
195
votes
2 answers

Cannot move out of borrowed content / cannot move out of behind a shared reference

I don't understand the error cannot move out of borrowed content. I have received it many times and I have always solved it, but I've never understood why. For example: for line in self.xslg_file.iter() { self.buffer.clear(); for…
Peekmo
  • 2,843
  • 2
  • 19
  • 25
194
votes
8 answers

How to "return an object" in C++?

I know the title sounds familiar as there are many similar questions, but I'm asking for a different aspect of the problem (I know the difference between having things on the stack and putting them on the heap). In Java I can always return…
phunehehe
  • 8,618
  • 7
  • 49
  • 79
194
votes
11 answers

C# string reference type?

I know that "string" in C# is a reference type. This is on MSDN. However, this code doesn't work as it should then: class Test { public static void Main() { string test = "before passing"; Console.WriteLine(test); …
CriosR
194
votes
2 answers

How to cast/convert pointer to reference in C++

How can I pass a pointer (Object *ob) to a function which prototype is void foo(Object &) ?
Dewsworld
  • 13,367
  • 23
  • 68
  • 104
192
votes
7 answers

C++: const reference, before vs after type-specifier

What is the difference between the arguments in: int foo1(const Fred &arg) { ... } and int foo2(Fred const &arg) { ... } ? I don't see this case covered in the parashift FAQ.
eisbaw
  • 2,678
  • 2
  • 19
  • 18
186
votes
3 answers

Should I pass an std::function by const-reference?

Let's say I have a function which takes an std::function: void callFunction(std::function x) { x(); } Should I pass x by const-reference instead?: void callFunction(const std::function& x) { x(); } Does the answer to this…
Sven Adbring
  • 1,961
  • 2
  • 12
  • 3
180
votes
14 answers

Why are arrays of references illegal?

The following code does not compile. int a = 1, b = 2, c = 3; int& arr[] = {a,b,c,8}; What does the C++ standard say about this? I know I could declare a class that contains a reference, then create an array of that class, as shown below. But I…
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
173
votes
21 answers

Namespace not recognized (even though it is there)

I am getting this error: The type or namespace name 'AutoMapper' could not be found (are you missing a using directive or an assembly reference?) The funny thing is that I have that reference in my project already: And this is my code: using…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
169
votes
18 answers

Which is better, return value or out parameter?

If we want to get a value from a method, we can use either return value, like this: public int GetValue(); or: public void GetValue(out int x); I don't really understand the differences between them, and so, don't know which is better. Can you…
Delta76
  • 13,931
  • 30
  • 95
  • 128
164
votes
5 answers

PHP Foreach Pass by Reference: Last Element Duplicating? (Bug?)

I just had some very strange behavior with a simple php script I was writing. I reduced it to the minimum necessary to recreate the bug:
regality
  • 6,496
  • 6
  • 29
  • 26
159
votes
8 answers

Can I pass parameters by reference in Java?

I'd like semantics similar to C#'s ref keyword.
ripper234
  • 222,824
  • 274
  • 634
  • 905