导航

C++ read record

Copy-construction
 The problem occurs because the compiler make an assumption about how to create a object from an existing object. When you pass a object by value, you create a new object, the passed object inside the function frame, from an existing object, the original object outside the function frame. This is often true when returning an object from a function( The return values does not assign to another objects, the Copy-construction will be called.).
When the beginning of the return from f(), the local variable can be destroyed, it must be copied into the return value. If the return value will be storied in a object( example:
A  b = fun( a );), the copy-contruction will stored the value into the return-scope directly. And the operator= will not be called.
But when the returning value will not occur, the compiler will create a temporary object storage to save the "return value", and it will be destroyed in the end of the funtion.


Pointer to class Functions:
 Parentheses also play an important role when defining and using poiters to member functions. If you have a function inside a class,  you define a pointer to that member function by inserting the class name and scope resolution operator into an ordinary function
pointer defination:
class simple
{
 public:
  int f( float ) const { return 1;}
};
int ( simple::*fp)( float) const;
int ( simple::fp2)( float ) const = &simple::f;
int main()
{
fp = &simepe2::f;
}


Three: the arguments & return values
  1. As with any function arguement, if you only need to read from the arguement and not
  change it, default to passing it as a const reference. Ordinary arithmetic   operations( like + and -, etc.) and Boolean will not change their arguements, so   pass the function is predominantly what you'll use.
  When the function is a class member, this tranlates to making it a const member   function. Only with the operator-assignments argument, is the left argument not a
  constant, but it's still passed in as an address because it will be changed.
 2. The type of return value you should select depends on the expected meaning of the   operator.( Again, you can do anything you want with the arguments and return   values.) If the affect of the operator is to produce a new value, you will need to   generate a new object as the return values. The object is returned by values as a
  const, so the result can not be modified as an l-value.
 3.  All the assignment operator will modify the l-value. To allow the result of the
  assignment to be used in chained expressions, like a = b = c, it's expected that
  you will return a reference to that same lvalue that was just modified.
  But should this reference be a const or nonconst?
  The return value for all of the assignment operators should be a nonconst reference
  to the l-value.
  

     
        

posted on 2005-04-04 16:18  Raker  阅读(543)  评论(0)    收藏  举报