[Qt-interest] Implicitly Shared Classes

Tom Browder tom.browder at gmail.com
Sun Feb 15 14:03:33 CET 2009


I have a question about the efficiency of passing an implicitly shared
class around.  Consider function foo which returns a map apparently
generated on the stack versus function foo2 in which the caller
provides an existing map.

QMap<int> M foo() {
  QMap<int> m;
  // ... put a lot of integers in m
  return m;
}

void foo2(QMap<int>& M) {
  // ... put a lot of integers in M
}

int main(int argc, char** argv) {
  QMap<int>  M = foo;  // 1. is m copied from m in foo as it goes out of scope?
  QMap<int>  M2;
  foo2(M2);                  // 2. is this more efficient than 1?
}

In situation 1 is data copied from m in foo as it goes out of scope or
does M just take over the data?

Are both situations then equally efficient?

Thanks.

-Tom



More information about the Qt-interest-old mailing list