answersLogoWhite

0


Best Answer

For an object named Point you would declare an object reference variable like this:

Point p1;

The name p1 can be any valid variable name that you want. Note that p1 does not yet refer to an actual object yet, you have just created a reference to one (kind of like a pointer in C). To set p1 to actually refer to an instance of the object Point you could do something like this:

p1 = new Point();

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

You create a reference with the reference operator (&).

int val = 42; // normal variable

int& ref = value; // reference to val

Note that val and ref refer to the same object, ref is simply an alias for val. If you alter the value of the ref you alter the value of val, and vice versa.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

There's no such thing as a reference variable. A variable is an object that has identity (an address). Variables in static memory or on the stack (local memory) can be named, however objects on the heap (free store) cannot be named because they do not exit at compile time, they only exist at runtime, so they are anonymous. However, any object in memory, whether named or anonymous, has identity because we can take its address.

A reference is not an object of any kind (it has no identity), it is simply an alternate name or alias for another object that exists in memory (one that has identity). References are a bit like pointer variables in that they can both refer to something in memory, however pointers are variables so we can take the address of a pointer and store it in another pointer (creating a pointer to pointer variable). References don't have identity so if we try to take the address of a reference we end with the address of the object being referred to. This would be akin to taking the value of a pointer variable. More importantly, a pointer variable can refer to the NULL address (meaning "no object") whereas a reference cannot; a NULL reference would invalidate the program.

Not all languages support references. C is a prime example. Although we often use the term reference when referring to a pointer variable in C, this is only because C predates the concept of a native reference type. C++, on the other hand, does support a native reference type. Consider the following:

int i = 0;

int& ref = &i;

ref = 42;

assert (i == 42);

Here, both i and ref refer to the same object. Referring to an object in the same scope is pointless; it already has a perfectly good name, there's no need to give it another (it would only lead to confusion). References come into their own when we pass objects to functions because the function cannot know the name of the object (the actual argument) being passed:

void f (int& n) {

n = 0;

}

f (i);

Here, the function refers to i by the formal argument name n, which is a reference. Thus i and n both refer to the same object. If we change the value of n we change the value of i.

We can achieve the same sort of thing with pointer variables:

void g (int* n) { if (n!=NULL) *n = 0; }

g (&i);

Here we can see the main difference between a reference and a pointer variable. With a pointer variable we must be sure we are referring to something valid before we attempt to dereference the object being referred to. A reference is guaranteed to be non-null so we can eliminate many unnecessary tests for NULL from our code. We also get the advantage of a much simpler syntax; we don't have to dereference references!

Getting back to the question, how do we declare an array reference? First, we have to declare the array:

int x[3] {5, 10, 15};

The array's name is x and it has the type int[3]. There are basically two ways to declare a reference of this type:

int (&r)[3] = x; // the hard way

auto& r = x; // the easy way (since C++11)

The first way is only hard because we have to be explicit.

The biggest problem with arrays is that they decay to temporary pointers as soon as we try to copy them via assignment. This makes it impossible to pass arrays into functions by reference; in effect we have to refer to a pointer to the array:

void h (int* const& rpa) {

// ...

}

While this would work, the conversion to pointer means we lose all size information. This means we must pass the size through another parameter. However, a reference to a pointer only guarantees that the pointer variable is valid, it does not guarantee the pointer refers to a non-NULL address. Given that, we might as well just pass the pointer add array size just as we would in C:

void h (int* p, size_t size) {

if (p==NULL size==0) return; // safety-first!

// ...

}

We don't have this problem with resource handles like std::array because only built-in arrays decay to pointers. A std::array object can be referred just as easily as any other (non-array) variable:

template<typename T, size_t N>

void h2 (std::array<T, N>& a) {

if (a.size() == 0) return; // safety-first!

// ...

}

std::array<int, 3> z = {5, 10, 15};

h2 (z); // pass array by reference!

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you declare an object reference variable?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Why you use the reference variable?

The reference variable controls an object. Without the reference variable, you would have no way of accessing the object.


How do you make variables in game maker 7?

Depends, if you wish to create a variable that is only controlled by one object, then simply declare: "variable_name=x" with x being whatever value. Although, if you want a variable that is controlled by any object, then name the variable: "global.variable_name" the global part insures the variable will be available for reference to any object.


Difference between reference variable and instance variable?

An object is the actual storage space in memory in which some collection of data resides.A reference variable is a variable which refers to the memory location of an object.Look at the pseudocode below:Object obj = new Object();Here obj is the reference variable, and the data to which it refers is the object.


What happens if a final keyword is applied to identifier?

The final keyword indicates that a variable (identifier) can not change his value. In case the variable refers to a reference variable (an object) the values of variables inside (the object) can change but the reference can be reassigned (to another object).


What type of reference is C19?

A reference variable is used to refer to or give access to an object. A reference variable is declared to be of a particular type and that type can not be altered.


What is reference data types in java?

A reference variable is used to refer to (or access) an object. A reference variable is declared to be of a specific type and that type can never be changed. Ex: ArrayList lst = new ArrayList(); The above line creates a reference variable lst which refers to an ArrayList object


Declare the variable longPtr to be a pointer to an object of type long?

long *longPtr;


How do you return an object in java?

With the command return, followed by an object variable. In the method header, you have to declare the return type as the class of the object.


What is a variable used for in Python?

A variable is used to store information. As an example say you are asking someone for their favorite food, and want to store the answer in a variable called favoriteFood: favoriteFood = input("What is your favorite food?") Now if you print the value of favoriteFood (using print(FavoriteFood)) you will see that it has been saved in that variable. You can store any type in a variable, such as number, a string, or even an object.


What is reference variable and explain its uses?

Reference variable is an alias name for a previously defined variable. Its use is that the same data object can be referred by 2 names and then can be used interchangeably.


Explain reference variable and how it is different from pointer variable?

In JAVA, all variables are reference variables, and there are no pointer variables. Even though the platform may implement them as pointers, they are not available as such. In C, no variables are reference variables. They are a C++ enhancement. In C++ a reference variable is syntactically the same as a pointer variable, except that the use of the indirection operator (*) is implicit. You do declare reference variables slightly differently than pointer variables but, once you do so, they can be treated as non-pointer variables. Reference variables also cannot be redefined once they have been initialized to point to some object. They are const. Structurally, there is no difference between a pointer variable and a reference variable. They are both still pointers. The compiler just makes it easier to treat reference variables and non-pointer variables the same way.


What is a polymorphism in java programming?

Polymorphism can be considered as the ability of one thing being multiple other things (though partially or fully). Am I confusing you? I believe yes. To put it in simpler words, any java object that can pass more than one Is-A test can be considered polymorphic. Other than objects of type Object, all Java objects are polymorphic in that they pass the IS-A test for their own type and for class Object. Remember that the only way to access an object is through a reference variable, and there are a few key things to remember about references: &acirc;&euro;&cent; A reference variable can be of only one type, and once declared, that type can never be changed (although the object it references can change). &acirc;&euro;&cent; A reference is a variable, so it can be reassigned to other objects, (unless the reference is declared final). &acirc;&euro;&cent; A reference variable's type determines the methods that can be invoked on the object the variable is referencing. &acirc;&euro;&cent; A reference variable can refer to any object of the same type as the declared reference, or-this is the big one-it can refer to any subtype of the declared type! &acirc;&euro;&cent; A reference variable can be declared as a class type or an interface type. If the variable is declared as an interface type, it can reference any object of any class that implements the interface.