• Register
0 votes
28 views

Problem :

A simple question for which I want to find the answer here.

“As parameters to a function, class objects can be passed by reference only.”

void myRefFunction(type myRefVariable)
{
}

void main()
{
    myRefFunction(myRefVariable);
}

For a simple data types like intfloat, etc. a function is called by value.

But if myRefVariable is the array, only a starting address is passed (even though the function is the call by value function).

If myRefVariable is the object, also only a address of a object is passed rather than creating the copy and passing it.

So question is does C++ pass the object by reference or value?

7 5 2
3,870 points

1 Answer

0 votes

Solution :

Arguments are passed by the value, unless a function signature specifies it  otherwise:

·In a void foo(type arg), arg is passed by the value regardless of whether type is the simple type, the pointer type or a class type,

·In a void foo(type& arg), arg is passed by the reference.

In case of the arrays, a value that is passed is the pointer to a first elements of a array. If you know a size of an array at compile time then you can pass the array by reference as well: void foo(type (&arg)[10]).

9 7 4
38,600 points

Related questions

0 votes
1 answer 14 views
14 views
Problem: I am currently working on getting the HTML-based recursive directory listing based on the PHP code. My PHP code runs fine but it throws few errors: Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\directory5.php on line 34 Strict Standards: Only ... "".str_replace("\\", "/",     $directory)."/$value\">".$displayName($fileName, $extension)."</a></li>\n";  }
asked Jul 31 Raphael Pacheco 4.9k points
0 votes
2 answers 23 views
0 votes
1 answer 5 views
5 views
I want to know how to call to an overloaded function be ambiguous.
asked Oct 4 Daniel Anderson 4k points
1 vote
1 answer 622 views
622 views
Problem : I am very new to the C++programming, and so I keep facing the below error saying: Reference to overloaded function could not be resolved; did you mean to call it? Below is my code that is causing me all sorts of troubles : #include <stdio.h> #include < ... have multiple files, all of the files fail to work. Can anyone explain me why my code is failing and can be the solution for it?
asked Dec 30, 2019 alecxe 7.5k points