modifyByValue vs modifyByReference

Pages: 12
What the difference between these two, with some examples in support.

Thank you
The difference is that neither exists.

Maybe you meant pass-by-value and pass-by-reference?

In that case, the main difference is this:
1
2
3
4
5
6
7
8
void change1(int v)
{
    v = 3;
}
void change2(int &v)
{
    v = 4;
}
1
2
3
int x = 1, y = 2;
change1(x); //x is still 1
change2(y); //y is now 4 


It is faster to pass large objects like vectors by reference and const reference because they do not have to be copied, but it is actually slower to pass primitives by reference, so don't unless you need to change the value. Always pass large objects by const T &t unless you need to change their value, in which case it's just T &t.
Last edited on
pass by value takes a copy of the variable's value and manipulates it. No matter what happens to the copied value the original value remains unchanged. When the variable is passed by reference it is the original that is being manipulated which may change the original value to something else.
here's an example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void f(int,int&);//proto

int main()
{//tests the f()function
int a=22,b=44;
cout<<"a = "<<a<<" , b = "<<b<<endl;//prints a=22, b=44
f(a,b);
cout<<"a = "<<a<<" , b = "<<b<<endl;//prints a==22, b=99
f(2*a-3,b);
cout<<"a = "<<a<<" , b = "<<b<<endl;//prints a=22, b=99
}

void f(int x,int&y)
{//changes reference arguement to 99
x=88;
y=99;
}

Here is something from a book 1:
passing by value
int x;
-the parameter x is a local variable
-it is a duplicate of the argument
-it cannot change the argument
-the argument passed by value may be a constant,
a variable or an expression
-the argument is read-only

passing by reference
int &x
-the parameter x is a local reference
-it is a synonym for the argument
-it can change the argument
-the argument passed by reference must be a variable
-the argument is read-write

1 Programming with C++ John R Hubbard,Ph.D.
const int &x
-the parameter x is a local reference
-it is a synonym for the argument
-it can not change the argument
-the argument passed by reference need not be a variable
-the argument is read-only
to LB ...read your two posts with interest and now have a much better understanding.
Why in creating copy constructor the parameter must be passed by reference and not by value?
It isn't a must, it's just very recommended. But you have to pass by const reference if you want to be able to construct an instance from an existing constant one.

const-correctness has to do with guarantees about not changing an object's state, and in C++ the const keyword just helps the programmer make sure of that before running the code; it is a compile time safety feature.


I misunderstood Napster. See ne555's posts below.
Last edited on
@Napster12 ¿when do you think that a copy constructor is called?
A copy constructor is called when you need to initialize an object from an existing one.
foo::foo( foo obj ); ¿how do you think that obj will be initialized?
closed account (zwA4jE8b)
i am intersted to the answer of that one ne555.
It looks like it is an infinite loop recursion. lol
Last edited on
It doesn't even compile (thank god!): http://ideone.com/VG4P6
Last edited on
closed account (o1vk4iN6)
He mean's you can't pass by value, as it creates a copy of the class, if the copy constructor hasn't been defined yet.
...that's deep.
He mean's you can't pass by value, as it creates a copy of the class, if the copy constructor hasn't been defined yet.


I don't see how when it is defined has anything to do with it? It would be infinite recursion if it did compile.
Last edited on
WHAT IS THE FUNCTION OF THE COPY CONSTRUCTOR?
IT CONSTRUCTS AN OBEJCT AS A COPY OF ANOTHER.
Can you tell tell me what's wrong with this one:

#include <iostream>
using namespace std;
void printArray(int theArray[], int sizeOfArray);


int main (){
int pablo [4] = {22, 18, 15, 34};
int marco [5] = {43, 23,54, 26, 78};

printArray(pablo, 4);
printArray(marco, 5);
}
void printArray(int theArray[], int sizeOfArray){
for(int x=0; x<sizeOfArray; x++);{

cout << theArray[x] <<endl;
}

}

error C2065: 'x' : undeclared identifier: THAT'S THE ERROR MESSAGE I GOT IN VISUAL C++ 2010.

for(int x=0; x<sizeOfArray; x++);{.

.                                ^
That semicolon is throwing it off.
Last edited on
I just found out. Thanks L B.

I NEED TO KNOW WHY CLASSES HAVE TO BE CREATED OUTSIDE THE MAIN FUNCTION.
Pages: 12