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
402
votes
16 answers

Is the practice of returning a C++ reference variable evil?

This is a little subjective I think; I'm not sure if the opinion will be unanimous (I've seen a lot of code snippets where references are returned). According to a comment toward this question I just asked, regarding initializing references,…
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
350
votes
12 answers

When should I use the new keyword in C++?

I've been using C++ for a short while, and I've been wondering about the new keyword. Simply, should I be using it, or not? With the new keyword... MyClass* myClass = new MyClass(); myClass->MyField = "Hello world!"; Without the new…
Nick Bolton
  • 38,276
  • 70
  • 174
  • 242
333
votes
47 answers

How do I fix a "type or namespace name could not be found" error in Visual Studio?

I'm getting a: type or namespace name could not be found error for a C# WPF app in VS2010. This area of code was compiling fine, but suddenly I'm getting this error. I've tried removing the Project Reference and the using statement, shutting…
Greg
  • 34,042
  • 79
  • 253
  • 454
327
votes
4 answers

What are Rust's exact auto-dereferencing rules?

I'm learning/experimenting with Rust, and in all the elegance that I find in this language, there is one peculiarity that baffles me and seems totally out of place. Rust automatically dereferences pointers when making method calls. I made some tests…
kFYatek
  • 5,503
  • 4
  • 21
  • 14
325
votes
45 answers

Could not load file or assembly or one of its dependencies

I'm having another of these "Could not load file or assembly or one of its dependencies" problems. Additional information: Could not load file or assembly 'Microsoft.Practices.Unity, Version=1.2.0.0,…
ronag
  • 49,529
  • 25
  • 126
  • 221
313
votes
8 answers

Are arrays in PHP copied as value or as reference to new variables, and when passed to functions?

1) When an array is passed as an argument to a method or function, is it passed by reference, or by value? 2) When assigning an array to a variable, is the new variable a reference to the original array, or is it new copy? What about doing this: $a…
Frank
  • 18,432
  • 9
  • 30
  • 30
298
votes
20 answers

MSBuild doesn't copy references (DLL files) if using project dependencies in solution

I have four projects in my Visual Studio solution (everyone targeting .NET 3.5) - for my problem only these two are important: MyBaseProject <- this class library references a third-party DLL file (elmah.dll) MyWebProject1 <- this web application…
toebens
  • 4,039
  • 6
  • 24
  • 31
287
votes
6 answers

Referencing a string in a string array resource with xml

I have preferences where you can enable/disable what items will show up on the menu. There are 17 items. I made a string array in values/arrays.xml with titles for each of these 17 items. I have preferences.xml which has the layout for my…
Jorsher
  • 3,683
  • 3
  • 17
  • 14
276
votes
12 answers

Pointer vs. Reference

What would be better practice when giving a function the original variable to work with: unsigned long x = 4; void func1(unsigned long& val) { val = 5; } func1(x); or: void func2(unsigned long* val) { *val =…
Jack Reza
  • 2,771
  • 3
  • 16
  • 4
275
votes
11 answers

Why are explicit lifetimes needed in Rust?

I was reading the lifetimes chapter of the Rust book, and I came across this example for a named/explicit lifetime: struct Foo<'a> { x: &'a i32, } fn main() { let x; // -+ x goes into scope …
corazza
  • 31,222
  • 37
  • 115
  • 186
253
votes
11 answers

How come a non-const reference cannot bind to a temporary object?

Why is it not allowed to get non-const reference to a temporary object, which function getx() returns? Clearly, this is prohibited by C++ Standard but I am interested in the purpose of such restriction, not a reference to the standard. struct…
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
248
votes
8 answers

How to reference generic classes and methods in xml documentation

When writing xml documentation you can use something, which works of course. But how do you reference a class or a method with generic types? public class FancyClass { public string FancyMethod(T value) { return…
Svish
  • 152,914
  • 173
  • 462
  • 620
238
votes
20 answers

Dependent DLL is not getting copied to the build output folder in Visual Studio

I have a visual studio solution. I have many projects in the solution. There is one main project which acts as the start up and uses other projects. There is one project say "ProjectX". Its reference is added to main project. The ProjectX references…
Brij
  • 11,731
  • 22
  • 78
  • 116
230
votes
4 answers

Creating a copy of an object in C#

Please have a look at the code below (excerpt from a C# book): public class MyClass { public int val; } public struct myStruct { public int val; } public class Program { private static void Main(string[] args) { MyClass…
afaolek
  • 8,452
  • 13
  • 45
  • 60
226
votes
2 answers

Understanding exactly when a data.table is a reference to (vs a copy of) another data.table

I'm having a little trouble understanding the pass-by-reference properties of data.table. Some operations seem to 'break' the reference, and I'd like to understand exactly what's happening. On creating a data.table from another data.table (via <-,…
Peter Fine
  • 2,873
  • 3
  • 14
  • 16