Quantcast
Channel: User Peter - Reinstate Monica - Stack Overflow
Viewing all articles
Browse latest Browse all 223

Answer by Peter - Reinstate Monica for Pointer expressions: *ptr++, *++ptr and ++*ptr

$
0
0

Here is a class which documents all the relevant functions on stderr. I try the three expressions you mention in a program:

#include <iostream>#include <string>using namespace std;struct PtrT;ostream& operator<<(ostream& os, const PtrT& ptr);/// <summary>/// A class which defines post- and pre-increment as well as dereferencing./// It documents all function calls, including constructors and destructor./// </summary>struct PtrT{    string name; //< Provides an identity.    int val = 0;    PtrT(string n) : name(n)    {        cerr << "PrT(\"" << name << "\", " << val << "). ";    }    /// <summary>    /// Make a copy of the right hand side, but prefix the    /// new name with "copy_of"    /// </summary>    PtrT(const PtrT &rhs) : name("copy_of_" + rhs.name), val(rhs.val)    {        cerr << "PrT(" << rhs << "). ";    }    ~PtrT()    {        cerr << "~PrT(" << *this << "). ";    }    /// <summary>    /// Post-increment, incrementing val.    /// </summary>    /// <returns>A copy of *this, with the old val.</returns>    PtrT operator++(int)    {        cerr << "post-incr " << *this << ". ";        PtrT returnVal(*this);++val;        cerr << "post-incr: this is now " << *this << ", but will return " << returnVal << ". ";        return returnVal;    }    /// <summary>    /// Pre-increment, incrementing val    /// </summary>    /// <returns>*this with the incremented val.</returns>    PtrT& operator++()    {        cerr << "pre-incr " << *this << ", val will be " << val + 1 << ". ";++val;        return *this;    }    /// <summary>    /// "Dereferencing" operator    /// </summary>    /// <returns>A reference to val</returns>    int& operator*()    {        cerr << "deref " << *this << ". ";        return val;    }};/// <summary>/// Convenience function printing the name with val in parenthesis/// </summary>ostream& operator<<(ostream& os, const PtrT &ptr){    os << ptr.name << "(" << ptr.val << ")";    return os;}int main(){    PtrT p("p");    cerr << "Before assignment to i: " << p << endl;    cerr << "\n--------------- *++p ------------------\n";    // Increments p, returns ref to val after incrementing    int i = *++p;     cerr << "i now: " << i << ", " << p << endl;    cerr << "\n--------------- ++*p ------------------\n";    // return val, increment int through reference    i = ++*p;     cerr << "i now: " << i << ", " << p << endl;    cerr << "\n--------------- *p++ ------------------\n";    // make a copy of p, increment p.val, return ref to val of copy of p    i = *p++;    cerr << "i now: " << i << ", " << p << endl;}

The class output operator prints the name of the object with the value of its int member, val, in parentheses. The program's output is

PrT("p", 0). Before assignment to i: p(0)--------------- *++p ------------------pre-incr p(0), val will be 1. deref p(1). i now: 1, p(1)--------------- ++*p ------------------deref p(1). i now: 2, p(2)--------------- *p++ ------------------post-incr p(2). PrT(p(2)). post-incr: this is now p(3), but will return copy_of_p(2). deref copy_of_p(2). ~PrT(copy_of_p(2)). i now: 2, p(3)~PrT(p(3)).
  • The first case, *++p, is straight-forward: increment, dereference, done.
  • In the second case, ++*p, the increment is the built-in integer increment operator, applied to the result of the dereferencing, an int reference, not to the class (note that there is no increment operator output)! But because the reference points to the class member, the same effect is achieved: p.val is incremented, as p(2) shows.
  • The third case, *p++, is the most interesting. As is customary, the operator function internally increments the original object but returns a copy with the un-incremented value. That copy gets dereferenced, yielding the un-incremented val, and then destroyed. The original p, however, has value 3 afterwards when it is printed and, in the end, destroyed as well.

Viewing all articles
Browse latest Browse all 223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>