Anti-nuisance lawsuit warning: The purpose of these notes is to remind me, Zoegond, of stuff or to help me work stuff out. They may contain mistakes.

Quick

  • ($a, $b....) = unpack("A2A7...", $packed)
  • push( array, list )

Monday, October 10, 2011

Constructors, temporary objects and initialisation

With this setup (two constructors, a copy constructor and the destructor)

class A {
A() {...}
A(int i) {...}
A(const A &a) {...}
~A() {...}
};

{
A c = A(11);
}


results in one call to the int constructor and one call to the destructor, whereas

{
A d;
d = A(11);
}


results in one call to the no-argument constructor, then one call to the int constructor when the temporary object is created, then a call to the destructor when its statement is complete; finally another destructor call when d goes out of scope.

NB btw that although both these forms look like they have an assignment in them, because it's in a definition, an assignment is not actually performed (and any assignment operator is not called). This situation counts as an initialisation not an assignment.

Followers

Blog Archive