answersLogoWhite

0


Best Answer

Use the address-of operator (&). Note that the function must accept a pointer to the class type. If it accepts a reference or value then you cannot pass the object's address, you must pass the object itself.

User Avatar

Wiki User

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

Wiki User

10y ago

Question What is an object?

Answer An object is an instance of a class.

Question What are function arguments?

Answer Function arguments define the parameters to and/or from a function, whether to provide input to the function, output from the function, or both. Output arguments allow functions to return more than one value. The arguments you pass to a function are known as the actual arguments, whereas the arguments used by the function itself are known as the formal arguments. Arguments may be passed by value or by reference, however arguments passed by value cannot be used as output arguments, unless the argument is pointer. However, arguments can be further qualified with the const keyword.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Objects can be passed to member functions in C++ in a variety of ways. The function's parameter determines how the object is handled by the function. The following examples show all the possible parameter variations (the same variations apply to member functions as well as external functions):

By reference:

void foo(const MyObject& o);

void foo(MyObject& o);

By value:

void foo(const MyObject o);

void foo(MyObject o);

By pointer:

void foo(MyObject* p);

void foo(const MyObject* p);

void foo(MyObject* const p);

void foo(const MyObject* const p);

The pointer variations can be extended to include pointer to pointer variants and so on. However, it is not possible for any one function in the same namespace to be overloaded with all eight of these variations unless the return values are altered or dummy parameters are employed to eliminate the ambiguities. Under normal circumstances there will only be two overloads provided, one of which must accept a pointer. If additional overloads are provided, they must be pointer to pointer variants.

Which variations are actually provided depend upon how the function interacts with the object. If the parameter is declared const then the function can only call the object's const member functions and access its mutable member variables. In the case of the pointer variations, this means those that declare const MyObject*parameters. Those with const p parameters only mean the pointer will always point at the same object within the function body.

Passing objects by reference is the preferred method of passing objects to functions whenever an object is guaranteed to be known to exist. Passing by pointer is often provided as an overload to cater for those cases where an object may not exist; in some cases this may in fact be the only function provided. Regardless, passing by reference or by pointer is effectively the same as passing the object itself. Thus if the parameter is declared non-const, any changes made to the function's parameter will be reflected in the original object.

Passing objects by value is rarely used and should be avoided as much as is possible. When you pass an object by value, the object's copy constructor is automatically invoked. This is often unnecessary and can have an enormous impact upon your program's performance and memory consumption, especially with complex object's that contain embedded objects and/or inherit from other classes, each of which must be copied. However, there will be occasions where a function needs to modify an object where those changes would be undesirable. Thus passing by value would be deemed acceptable. The alternative would be to manually copy your object and then pass the copy by reference or by pointer instead.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you pass the address of the object as function argument in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you pass enum object as argument to function in c?

You can't pass an enum as an argument to a function. An enum in C isn't an object, it's a type. All you can do is pass a variable that is of the particular enum's type.


Can you pass object as an argument to a method?

if you have a function or a method that takes Object as a parameter, you can call that function or method and pass an Object as follows: Let's say you have a class that extends Object and a function as follows public class A extends Object { ..... } void function ( A arg ){ ..... } To call this function, you need an instance of the class A A objectInstance = new A(); function(objectInstance); The next line shows how to pass an instance of the class A to the function.


Does C even have pass by reference?

Strictly speaking, no. All arguments in C are passed by value. However, when the argument being passed is a memory address, although the address itself is passed by value, we're effectively passing the object that resides at that address -- by reference. Thus when a function's formal argument is a pointer variable (of any type), then it can be taken as read that the function is using the pass by reference semantic rather than the pass by value semantic. Nevertheless, it is important to keep in mind that the formal argument is assigned a copy of the actual argument and is therefore being passed by value.


What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).


Call by reference in c?

If you are calling by reference it means that compilator will not create a local copy of the variable which you are referencing to. It will get access to the memory where the variable saved. When you are doing any operations with a referenced variable you can change the value of the variable.It is a technique whereby the called function receives values from its calling function, stores these values in its local arguments and returns a result. The called function does not have access to any local variables in the calling function. Anything passed into the function call is unchanged in the caller's scope when the function returns.No, C uses call-by-value. Of course you may use pointers as parameters.

Related questions

How do you pass enum object as argument to function in c?

You can't pass an enum as an argument to a function. An enum in C isn't an object, it's a type. All you can do is pass a variable that is of the particular enum's type.


Can you pass address of the structure member as a function argument?

Yes.


Can you pass object as an argument to a method?

if you have a function or a method that takes Object as a parameter, you can call that function or method and pass an Object as follows: Let's say you have a class that extends Object and a function as follows public class A extends Object { ..... } void function ( A arg ){ ..... } To call this function, you need an instance of the class A A objectInstance = new A(); function(objectInstance); The next line shows how to pass an instance of the class A to the function.


Does C even have pass by reference?

Strictly speaking, no. All arguments in C are passed by value. However, when the argument being passed is a memory address, although the address itself is passed by value, we're effectively passing the object that resides at that address -- by reference. Thus when a function's formal argument is a pointer variable (of any type), then it can be taken as read that the function is using the pass by reference semantic rather than the pass by value semantic. Nevertheless, it is important to keep in mind that the formal argument is assigned a copy of the actual argument and is therefore being passed by value.


What is difference between call by value and pass by value?

When you pass by value, the function's parameter is assigned the value of the argument that was passed. When you pass by reference, the function's reference parameter is assigned the address of the argument. In other words, pass by value copies the value, pass by reference passes the variable itself.Pass by reference is always the preferred method of passing arguments to functions when those arguments are complex objects. If the argument is a primitive data type then the cost in copying the value is minimal, but copying a complex object is expensive in terms of performance and memory consumption. If the function parameter is declare constant, you can be assured the object's immutable members will not be affected by the function. If it is non-constant and you do not wish your object to be altered, you can either copy the object and pass the copy, or pass the object by value if the function is overloaded to cater for this eventuality (good code will provide both options).


Call by reference in c?

If you are calling by reference it means that compilator will not create a local copy of the variable which you are referencing to. It will get access to the memory where the variable saved. When you are doing any operations with a referenced variable you can change the value of the variable.It is a technique whereby the called function receives values from its calling function, stores these values in its local arguments and returns a result. The called function does not have access to any local variables in the calling function. Anything passed into the function call is unchanged in the caller's scope when the function returns.No, C uses call-by-value. Of course you may use pointers as parameters.


What do you mean by call value and call by reference?

Call by value and call by reference relate to the way in which arguments are passed into functions. The function itself determines which method is employed. When a function expects an argument by value, the function makes a copy of the argument you pass. That is, you are passing the argument's value, not the argument itself. The value you pass is subsequently assigned to the function argument. If the function argument is an object (an instance of a class), then that class' copy constructor is automatically invoked and the copy is assigned to the function argument. The function argument remains in scope until the function returns, which then invokes the class' destructor. Since the function works with a copy of the argument that you passed, the function has no direct effect on the argument you passed. Passing large and complex objects by value is clearly costly in terms of both performance and memory, but if you want the function to actually make changes to your object, then pass by value will not work unless you also return a copy of the altered object and assign this copy to your original object. That's two copy constructors, two destructors, and one assignment. This is where pass by reference comes in. When you pass by reference you are passing the address of your argument to the function, and that address is assigned to the function argument. So unless the function argument is declared const, any changes made to the function argument will be reflected in the argument you passed. This is clearly more efficient than passing by value, since no copies need to be made. And when the function argument is declared const, you can be assured that your object's immutable members will not be altered by the function since the function can only call const member functions. Passing by reference is clearly the preferred method of passing arguments into functions, particularly when those arguments are complex objects. In order to achieve pass by reference, the function argument needs to accept an address. Assigning the address to a pointer argument seems the obvious way of doing this, but in C++ there is a better way: use a reference! C programmers often get confused when we speak of C++ references because a reference in C is simply another name for a pointer. But in C++, a reference is not a pointer at all. Keep in mind that a pointer is a variable (or a constant), and therefore a pointer has an address of its own because it needs somewhere to store the address that it is pointing at. The stored address is the pointer's value, and that value can be dereferenced provided it is not NULL. That is, dereferencing a pointer gives us the indirect value stored in the address that the pointer is referring to. Moreover, pointer variables can be assigned any address at any time; they can point anywhere, or they can point to NULL. In C++, however, references behave differently to pointers. Once assigned, you cannot reassign a reference and they can never be NULL. Therefore you must assign a reference to an exiting object at the point of instantiation; you cannot declare and assign separately as you can with a pointer. This is because references do not store the address they refer to, they are simply aliases for the address itself. In other words, the address of a reference is the same as the reference itself. In many ways, a reference is not unlike a constant pointer, since it always refers to the same object, but even a constant pointer has its own address, a reference does not. Now that we understand what a reference is, we can see how it can be applied to functions that expect an address. When we pass our argument to a pass by reference function, we are assigning the address of the argument to the function argument, just as we would if the function argument were a pointer. The difference is that we don't need to use the address-of operator to obtain the address since the function already knows to expect a reference. That address is then assigned to the function argument. While the argument remains in scope, we have two guarantees: the reference cannot be NULL (so we don't need to test for NULL as we would with a pointer) and the reference cannot be reassigned (so its effectively the same as a const pointer). Moreover, since it is not a pointer of any kind, there is no redirection involved. The reference can be treated just as if it were the argument we passed; they are one and the same after all, just different names. To conclude, here are some examples of functions that employ pass by value and pass by reference: Example 1: pass by value MyObject foo(MyClass val) { val.set_data(42); return(val); } In this example we are passing a complex object of type MyClass that has a set_data() method. The object we pass is left unaffected because val is a copy of the object we passed. However, if we placed trace code in the class copy constructor and the destructor, we'd find two copies are constructed and destroyed during this call, once to create val, and again to create the return value. val is destroyed first and the return value second. The return value is an automatic variable which we can immediately assign to another object (including the original argument we passed to the function if we wished) but beyond that the return value will cease to exist. To call this function and assign the return value to the original object, we'd use the following: MyClass x; x = foo(x); Example 2: pass by reference (via pointer) void foo(MyClass* const ptr) { if( ptr ) { ptr->set_data(42); // (*ptr).set_data(42); } } Note that the points-to operator (->) is simply sugar-coating for the dereference operator, as shown in the commented line. Note also that the const keyword applies to the pointer itself, not what it points to. Thus *ptr is variable, but ptr is not. Here we're passing a reference by const pointer. Since pointers may be NULL we must ensure the pointer is non-NULL before we attempt to dereference it. Note that we do not need to return anything since ptr is assigned the address of the argument we passed, thus any changes made to the pointer's indirect value are reflected in the argument. In other words, ptr is an output argument. In order to call this function, we must employ the address-of operator. E.g., MyClass x; foo(&x); Example 4: pass by reference void foo(MyObject& ref) { ref.set_data(42); } Here we can see the C++ way of passing by reference. Unlike pointers we don't need to test for NULL, nor do we need to dereference. ref is guaranteed to always refer to a valid object. Moreover, the function call itself is simplified because we don't need to specify the address-of operator. MyClass x; foo(x); Note that ref is simply an alias for x. If we examine the address-of ref (&ref) we will find it is exactly the same as the address-of x (&x). If we look at the address-of ptr (&ptr) in the previous example, we'd find that it was not the same as &x, but its value would be &x, proving that references are neither pointers nor variables of any kind. Note that although these examples use complex objects, the same principal applies to primitives. The only difference is that there is no penalty in passing primitives by value as it's just as quick to pass by value as it is to pass by reference. It's just a question of whether you want changes reflected in the original argument or not. As a final note, if a function expects pass by reference, but you do not wish changes to be reflected in your object, copy the object first and pass the copy instead. This is really no different to pass by value. However, if the function argument is a constant reference, you don't need to make a copy since the function cannot alter the object's immutable members. You can also overload functions to pass by value and by reference, giving the best of both options. However, to achieve this, you need to use the pointer version of pass by reference. This is because the compiler cannot differentiate pass by value from pass by reference any other way.


How structure is passed to function using structure pointer?

When you pass an object to a function you are not actually passing the object, you are only passing the object's value. This is what is meant by the term pass by value.When passing a value to a function there are actually two objects involved in the exchange: the actual argument (the object that is being passed) and the formal argument (the object used by the function). When we call a function that accepts one or more arguments (also known as parameters), the value of the actual argument is assigned to the corresponding formal argument. Thus the formal argument is a copy of the actual argument and any changes made to the formal argument will have no effect upon the actual argument.When the formal argument is a pointer, however, the value we pass is a memory address. The actual argument can either be a pointer of the same type or we can take the address of an object of the same type using the address-of operator and pass that. Either way, the value we pass is a memory address. We call this pass by reference even though the address is actually being passed by value. Passing by reference means that the formal argument and the actual argument both refer to the same object and offers an efficient means of passing large objects that are too expensive to copy. This includes most structures and unions and all arrays. Note that arrays implicitly decay to pointers and therefore cannot be passed by value. Structures and unions can be passed by value, but if they are larger than a pointer (in bytes) passing by reference is more efficient.There are four different ways to declare a formal argument as a pointer:mutable pointer to mutable typemutable pointer to constant typeconstant pointer to mutable typeconstant pointer to constant typeIdeally, functions should declare formal pointer arguments using methods 2 or 4. Both point to constant types so this gives the caller an assurance that the function has no side effects upon the object being passed by reference. Functions that use methods 1 or 3 should be regarded as having side-effects upon the object being referred to. This can be desirable for efficiency reasons, however returning values via arguments (also known as output parameters) should be avoided whenever possible.Note that it makes no difference if the formal pointer argument is mutable or constant because the formal and actual arguments are still separate objects. Constant formal arguments are only of relevance to the function designer, they are of no importance to the caller. This is true of all values passed to functions whether the value is a memory address or not. What is important to the caller of a by reference function is whether or not the object being pointed at is declared constant or not.


What is the advantages of call by reference?

Whenever you pass a value to a function, that value must be copied. If the value is large or complex, this can hinder performance, particularly when the function does not need to alter the value. To improve performance, we use the pass by reference semantic. Rather than passing the value itself, we pass the address of the value. That is, the address is copied, not the value. The function can then refer to the value by dereferencing the address. Ideally, pass by reference should only be used when the function does not need to alter the value. This is achieved by declaring the function's formal argument a pointer to constant type. This makes it clear to the caller the value of the actual argument will not be altered by the function. In some cases, we do require the function to alter the value. In these cases the argument is regarded as being an output parameter because it allows the function to return another value besides the return value. Typically, the caller will allocate some memory for the function to use (perhaps initialising it with a value), and then pass the address of that memory to the function. The function's formal argument is therefore declared a pointer to non-constant type, making it clear to the caller that the function will modify the value being pointed.


What is actually passed to a function when an object is passed to a function through a pointer?

When an object is passed to a function by pointer, a copy of the pointer's value is passed to the function. Pointers are always passed by value, but because the value is a memory address than can be dereferenced, they enable us to pass objects by reference. In languages such as C which have no concept of references, this was the only way to pass by reference. C++ introduced proper references (aliases for existing objects), thus when we want to pass by reference we can choose to use a pointer or an actual reference. Normally we'd only pass by pointer when passing an optional argument that defaults to NULL when no argument is given. Otherwise we pass by reference because references can never be NULL.


Why you pass arguments to function in c plus plus?

You pass arguments to functions because that is how you tell the function what you want it to do. If you had, for instance, a function that calculated the square root of something, you would pass that something as an argument, such as a = sqrt (b). In this case sqrt is the function name, b is passed as its argument, and the return value is assigned to a.


Is call by reference and pass by reference same thing?

Strictly speaking there is no such term as call by value. Functions are called while parameters are passed. When someone uses the term call by value they really mean pass by value, so in that sense they are the same. However call by value is incorrect terminology.