• Register
0 votes
58 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 20 views
20 views
Problem: Hi there! I just took an exam of programming in which there was a multiple-choice question that said: “When an array is passed to a function, it is interpreted as _____”: Address of the first element of the array. The number of elements of the array. Address ... 5,4,3,2,1. I did not understand the output. Can anybody please explain it? Also, tell the answer to the above MCQ.
asked Dec 24, 2020 Code Learner 5.3k points
0 votes
1 answer 24 views
24 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, 2020 Raphael Pacheco 4.9k points
0 votes
2 answers 26 views
0 votes
1 answer 8 views
8 views
I want to know how to call to an overloaded function be ambiguous.
asked Oct 4, 2020 Daniel Anderson 4k points
0 votes
1 answer 6 views
6 views
Problem: My arrangement is xy[n][2]in which the user enters a number nof rows, and the number of columns are 2, but when the program is going to give me the numbers already ordered, its Y coordinate is altered, that is, it does not go with respect to the X. ... to understand, I was researching in google but I have not found anything to help me. I would appreciate your support, it is for a project.
asked Dec 3, 2020 sasha 5.3k points
0 votes
1 answer 58 views
58 views
Problem: Hello! I am a new programmer and just started learning C++ about 2 weeks ago. I was working with labels and was trying to run following program when I encountered the above mentioned error: #include<iostream> #include<string> using namespace std; int main() ... what is wrong with my program due to which this error is occurring. If someone knows, kindly help me solving this error. Thanks!
asked Nov 28, 2020 Code Learner 5.3k points
1 vote
1 answer 978 views
978 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
0 votes
1 answer 42 views
0 votes
1 answer 64 views
64 views
Problem : I am trying to import mySql database which was originally an access database into phpmyadmin and its giving me following error: incorrect table definition; there can be only one auto column and it must be defined as a key
asked Nov 14, 2019 peterlaw 6.9k points
0 votes
1 answer 19 views
19 views
I wonder if you could change the value of an external input variable that is treated as a function parameter, I remember seeing a way to do it but I don't remember very well. Suppose we use a function for the sum of two numbers, which has two parameters, the first ... of the sum =" << number << endl; The result of the sum should be stored in the variable. Would this procedure really work?
asked Oct 1, 2020 sasha 5.3k points