NoPredefines



next up previous
Next: Avoiding Friends Up: TECHNIQUES AND EXAMPLES Previous: SafeFloat

NoPredefines

  The operators operator=, operator& (address-of), operator, (sequencing) and the default and copy constructor all have default meaning. The operator= and the two constructors are discussed in the example of Section 3.1. This example shows making all the predefined operators ``private''. In particular, it discusses the operators operator& and operator, (the comma operator). Making operator& private prevents taking the address of an object (but allows objects to be passed to a function as a reference parameter). Making operator, private prevents an element of the class from appearing as the left operand of the comma operator.

class NoPredefines
{
private:
   NoPredefines(const NoPredefines &other);
   NoPredefines();
   NoPredefines* operator& ();
   void          operator, (void *);
   NoPredefines  operator= (const NoPredefines &value);

public:
   ...
};

Each line of the following function generates a compiler error because the above constructors and operators are private.

f(NoPredefines np, OtherClass oc)
{
    NoPredefines *p = &np;
    np, np;
    np, 1;
    np, 'c';
    np, oc;
}

An excellent example where the predefined default operator operator= has the wrong semantics is in the class String. The expected output of the following program is ``bye bye bYe bye'' (the assignment on Line [24] should affect s1 but not s2); however, using the default definition of operator= the output is ``bye bye bYe bYe'' because Line [24] affects both s1 and s2. The reason for this is that the default assignment operator operator=, which does a field by field assignment, causes s2.s to point to the same memory location as s1.s when the assignment on Line [21] is executed. This means that the update to s1 on Line [24] affects the value of s2. In contrast, using the assignment operator on Line [12], which copies the characters of the string not just the pointer to the string, the update to s1 on Line [24] does not affect the values of s2. (To simplify the example, operator= assumes there is enough space in the target string and the constructor assumes new does not return 0.)

[ 1]   class String
[ 2]   {
[ 3]   private:
[ 4]       char *s;
[ 5]   
[ 6]   public:
[ 7]       String(char *initial_value) 
[ 8]       {
[ 9]           s = new char [sizeof(initial_value)];
[10]           strcpy(s, initial_value);
[11]       };
[12]       String &operator=(String & rhs) {strcpy(s, rhs.s);return *this;};
[13]       void print() {printf(" %s ", s);};
[14]       char &operator[](int i) {return s[i];};
[15]   };
[16]   
[17]   main()
[18]   {
[19]       String s1 ("bye");
[20]       String s2 ("hello");
[21]       s2 = s1;
[22]       s1.print();
[23]       s2.print();
[24]       s1[1] = 'Y';
[25]       s1.print();
[26]       s2.print();
[27]   }



next up previous
Next: Avoiding Friends Up: TECHNIQUES AND EXAMPLES Previous: SafeFloat



David Binkley
Thu Feb 29 10:02:46 EST 1996