code snippets about copy constructor and temporary

#include "stdafx.h"
#include <stdio.h>

class CExample
{
public:
    int a;
    int b;

public:
    CExample()
 {
  printf("In default construction function!/n");
  a = 0;
  b = 0;
  printf("Addr: 0x%08X/n", this);
 };

    CExample(int m, int n = 0)
 {
  printf("In overloaded construction function!/n");
  a = m;
  b = n;
  printf("Addr: 0x%08X/n", this);
 };

    CExample(CExample& t)
 {
  printf("In copy construction function!/n");
  printf("Addr: 0x%08X/n", this);
  a = t.a;
  b = t.b;
 };

    virtual ~CExample()
 {
  printf("In destruction function!/n");
  printf("Addr: 0x%08X/n", this);
 };

public:
    int GetSum(CExample ts)
 {
  int locvar = ts.a + ts.b;
  ts.a = 1000;
  return locvar;
 }

 // tsarg is copy constructed before call FUNC;
 // tsarg is not temporary, but only a local variable.
 CExample GetModified(CExample tsarg)
 {
  CExample temp0 = tsarg; // copy constructor for copy initializer.
  CExample temp1(tsarg); // copy constructor

  printf("&tsarg = 0x%08X/n", &tsarg);
  printf("&temp0 = 0x%08X/n", &temp0);
  printf("&temp1 = 0x%08X/n", &temp1);

  temp0 = tsarg;   // default operate =, we can overload it.

  return temp0;
 }
};


int main(int argc, char* argv[])
{
    CExample tm(10, 20);
    printf("Sum = %d/n", tm.GetSum(tm));
    printf("tm.a = %d/n", tm.a);

 printf("&tm = 0x%08X/n", &tm);

 // copy constructor: oRet initialized before FUNC returned.
 // oRet is in main's Frame and is initialized in FUNC.
 printf("copy constructor: oRet initialized before FUNC returned:/n");
 CExample oRet = tm.GetModified(tm);
 printf("&oRet = 0x%08X/n", &oRet);

 // copy assignment: oRet assignment with one (unnamed)temporary
 // with default operate =.
 printf("copy assignment: oRet assignment with one (unnamed)temporary:/n");
 oRet = tm.GetModified(tm);

 // copy constructor: one (unnamed)temporary
 printf("copy assignment: one (unnamed)temporary:/n");
 tm.GetModified(tm);

 // reference of an temporary
 // is temporary's scope with rRet or disappear after FUNC?
 // answer: rRet object is in main's Frame and is initialized in FUNC. so in main's scope.
 CExample& rRet = tm.GetModified(tm);
 printf("rRet = 0x%08X/n", &rRet);
 printf("rRet = 0x%08X/n", rRet);

 const CExample& rcRet = tm.GetModified(tm);
 printf("rRet = 0x%08X/n", &rcRet);
 printf("rRet = 0x%08X/n", rcRet);

 return 0;
}


/*
There are three more cases in which an object is copied: as a function argument, as a function
return value, and as an exception. When an argument is passed, a hitherto uninitialized variable –
the formal parameter – is initialized. The semantics are identical to those of other initializations.
The same is the case for function return values and exceptions, although that is less obvious. In
such cases, the copy constructor will be applied. For example:

  s t r i n g g (s t r i n g a r g )
  {
  r e t u r n a r g ;
  }
  i n t m a i n ()
  {
  s t r i n g s = "N e w t o n ";
  s = g(s) ;
  } 

Clearly, the value of s ought to be " N e w t o n " after the call of g (). Getting a copy of the value of s
into the argument a r g is not difficult; a call of s t r i n g ’s copy constructor does that. Getting a copy
of that value out of g () takes another call of s t r i n g (c o n s t s t r i n g &); this time, the variable initialized
is a temporary one, which is then assigned to s . Often one, but not both, of these copy operations
can be optimized away. Such temporary variables are, of course, destroyed properly using
s t r i n g :: ~s t r i n g ()

*/

/*

copy constructor for exception reference rather than pointer or variable has much more therory behind the code.

*/

你可能感兴趣的:(code snippets about copy constructor and temporary)