• Register
0 votes
111 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

Please log in or register to answer this question.

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 18 views
18 views
Problem: Can someone tell me that is Java “pass-by-reference” or “pass-by-value”?
asked Mar 15 ummesalma 29.2k points
0 votes
1 answer 2 views
2 views
Problem: The primitive types (number, string, etc.) are passed by value, but objects are unknown, because they can be both passed-by-value (in case we consider that a variable holding an object is in fact a reference to the object) and ... to present the arguments passing conventions. Is there an excerpt from JavaScript specification, which defines what should be the semantics regarding this?
asked 2 days ago muktaa 34.4k points
0 votes
1 answer 8 views
8 views
Problem: c++ pass string by reference.
asked Apr 7 charles mathews 5.5k points
0 votes
1 answer 5 views
5 views
Problem : Can anybody please tell me pass by value and pass by reference in c?
asked Apr 4 Madhav96 2.3k points
0 votes
1 answer 5 views
5 views
Problem : Do anybody have answer for differncing between pass by value and pass by reference in php?
asked Apr 4 Madhav96 2.3k points
0 votes
1 answer 4 views
4 views
Problem: How can solve it? ... C# pass by reference vs value
asked Apr 4 Ifra 34.7k points
0 votes
1 answer 4 views
4 views
Problem: I always thought Java uses pass-by-reference. However, I've seen a couple of blog posts (for example, this blog) that claim that it isn't (the blog post says that Java uses pass-by-value)
asked Apr 2 ummesalma 29.2k points
0 votes
1 answer 6 views
6 views
Problem: I always thought that Java always passes by reference but I recently read a blog that said that Java uses pass by value. I am confused. Please help!
asked Mar 28 Code Learner 9.8k points
0 votes
1 answer 13 views
13 views
Problem: Can anyone please help me to solve it.. Java pass by value or reference
asked Mar 19 Ifra 34.7k points
0 votes
1 answer 5 views
5 views
Question Can anyone help me out here! What is pass by value and pass by reference in java
asked Mar 6 neeraj 10.5k points