PDA

View Full Version : Question about references in C++



jano_alex_es
15th January 2011, 21:31
Hello, having this simple code:


void myFunction(int& iAux){iAux = myINTMemberVariable;}

when it's executed, iAux is a copy of the integer or it's aimed to the adress in memory of the member variable?

I ask this because this is my usual way to obtain the private members for a class, and sometimes they have a huge QList of objects. In some programs I have this operation few times each few miliseconds... so it can be expensive.

Thanks!

javimoya
15th January 2011, 23:37
hi !
your example it's weird...
int myFunction() { return memberVariable; }

anyway...
in that function iAux is a reference to memory... (more or less like a pointer)....
so...it's cheap and fast. So... that's the way to go.
If you need to pass a variable by value: const int & iAux

----
now in spanish:
uff.. tio... menudo inglés que tienes !

Lykurg
16th January 2011, 07:18
You don't have to be afraid of passing a huge list by value. It is cheap because it is implicit shared. So it's like passing a pointer.

jano_alex_es
16th January 2011, 07:32
Thank you!


your example it's weird...
int myFunction() { return memberVariable; }

With simple type data I agree, but I prefer to use references instead of pointers when I have to pass lists, maps...

Javimoya: I feel sorry if my english is bad or something is difficult to understand... I'm recently working too much and this can affect my poor brain :P

javimoya
16th January 2011, 11:50
qlist and many other containers and classes in qt are a pointer to a data structure... so it's cheap and fast to return a qlist.
look for "implicit shared" in qt docs

Zlatomir
16th January 2011, 13:08
Don't confuse the implicit sharing with reference, and also don't confuse pointers with references:

The pointer is another variable that has as value an address of some object.

The reference is something like an alias (just another name) for the variable/object, and any change to reference will change the object itself.

The implicit sharing constructs another object that will share the same data with the first until it's modified, when you modify the implicit shared object you create a copy and the original remains un-modified.

Example:


#include <QString>
#include <QDebug>
#include "iostream"

void StrImpShared_funct (QString inStr){ //just take an qstring object by value, this will create an implicit shared object (since QString class uses implicit sharing)

//no copy of the data in made yet

inStr += "World"; //here, when we modify the implicit shared object it will copy the data from original and will modify the copy, and leave the original unchanged
qDebug() << "the ImplicitShared string is: " << inStr;
}

void StrRef_funct (QString& inStr){ //take a reference and add something to the qstring, this will modify the original
inStr += "World";
qDebug() << "the Reference string is: " << inStr;
}


int main()
{
QString str = "Hello ";
qDebug() << "the original string is: " << str << '\n';

StrImpShared_funct(str);
qDebug() << "the original string is still: " << str << '\n';

StrRef_funct (str);
qDebug() << "the original string is, now: " << str << '\n';

std::cin.get();
return 0;
}

stampede
16th January 2011, 21:43
You don't have to be afraid of passing a huge list by value. It is cheap because it is implicit shared. So it's like passing a pointer.
I hope you mean "passing a huge QList" ;)

IMHO, passing a QList, QString ect. by value is bad.
I know about implicit data sharing in QT, but this can introduce bad programming habits in general (especially for begginers).

javimoya
17th January 2011, 16:15
I hope you mean "passing a huge QList" ;)

IMHO, passing a QList, QString ect. by value is bad.
I know about implicit data sharing in QT, but this can introduce bad programming habits in general (especially for begginers).


totally agree.

wysota
17th January 2011, 19:45
IMHO, passing a QList, QString ect. by value is bad.
I know about implicit data sharing in QT, but this can introduce bad programming habits in general (especially for begginers).
Passing by reference is a much worse habbit and is ambiguous.

Take a look at this snippet, is "x" passed by value or by reference?

int x = 7;
doSomething(x);
qDebug() << x;

If you expect doSomething() to change "x", it is cleaner to pass it by pointer

int x = 7;
doSomething(&x);
qDebug() << x;

Some C++ purists will complain but the fact is it is a construct that doesn't force you to look at the declaration of doSomething().

If you don't want such constructs, you can always pass "x" by value or by a const reference and use the return value:

SomeType x = 7;
x = doSomething(x);

Compare the three situations and decide yourself which ones are clear and which are not. Especially the last syntax is where implicit sharing helps.

stampede
17th January 2011, 21:13
Passing by reference is a much worse habbit and is ambiguous.
I totally agree with this. I never pass arguments by reference if I want to change them inside method or function (because of the 'unclear' syntax reason, I prefer pointers for this) and always use const references instead of values (for complex datatypes). That's what I meant in my previous post.

wysota
18th January 2011, 12:19
I sometimes pass Qt objects (and PODs) by value and not by const reference if I expect that I might need to modify the value locally:

void setName(QString x) {
if(x.endsWith('\n'))
x.chop(1);
doSomething(x);
}
instead of:

void setName(const QString &x) {
QString y = x;
if(x.endsWith('\n'))
y.chop(1);
doSomething(y);
}

Besides, implicit sharing is so cheap that passing always by value is not a big problem.