When to use a link in C ++?

I always doubt when I should refer, and when I should use a variable pass. For example, one of the APIs is called by JOBJECTs -

QLIST retrive_cinemas(QString& options)
{
   return (staticobject::getcinemas(options));
}

or

QLIST retrive_cinemas(QString options)
{
   return (staticobject::getcinemas(options));
}
+3
source share
3 answers

It seems to me that your problem can be reduced to something like this:

You have a function / method f()and class X, and you want to know if / when Xto pass in f()by reference or not.

You can define three options:

void f(X v)            // #1 - pass by value
void f(const X& cr)    // #2 - pass by const reference (&)
void f(X& r)           // #3 - pass by reference (&)
  • If it’s Xcheap to copy (like a int, a double, etc.) and you don’t want to change it, then pass the value (# 1).

  • X (, a vector, a string, ..), yo , pass by const (# 2).

  • X f(), .


, , QString , , . a int double ( COW " ", , - - Win32 InterlockedIncrement() ), const (.. const QString &, # 2), .
(QString&, # 3), .

+4

Qt , , , implicit sharing :

++ Qt . , , , , --.

, , . !

. QString .

+2

Qt , . - ( , QString), , , , .

, , ( , QString , , , ( )), , , . , / , . , const.

0

All Articles