reference counting

Discussion in 'C++' started by Tony Johansson, May 22, 2005.

  1. Hello!

    I have these two class definitions below. Now to my question.
    This text is from a book that I say must be wrong.

    "The use of reference counters for the classes below brings up the question
    of whether the assignment statement should be shallow or deep; in other
    words, should you copy reference or values?
    Given the interface below, it does not matter, because the state of Point
    cannot be modified."

    I mean it's a big difference if you do a deep copy which mean that in this
    example the left side would get brand new Point object identical to the
    right side.
    But in the sentence above they say it doesn't matter but is that really
    true.

    //Tony


    class Point
    {
    public:
    point(double=0,double=0);
    virtual ~Point();
    double getX() const;
    double getY() const;
    private:
    double x_;
    double_;
    };

    class PointCounted
    {
    public:
    PointCounted(double=0, double=0); // creates a point
    PointCounted(const PointCounted&);
    PointCounted(const Point&);
    PointCounted& operator=(const PointCounted&);
    ~PointCounted();
    double getX() const;
    double getY() const;
    private:
    Point* p; //bridge to the implementetion
    int *count_; //reference counter
    };
    Tony Johansson, May 22, 2005
    #1
    1. Advertising

  2. On 2005-05-22, Tony Johansson <> wrote:
    > Hello!
    >
    > I have these two class definitions below. Now to my question.
    > This text is from a book that I say must be wrong.


    Oh boy. Get another book. You don't want to do a deep copy assignment with a
    reference counted class. Deep copy is one ownership strategy, reference counting
    is another one. By the way, what book is this ?

    Cheers,
    --
    Donovan Rebbechi
    http://pegasus.rutgers.edu/~elflord/
    Donovan Rebbechi, May 22, 2005
    #2
    1. Advertising

  3. Programming with design pattern!


    "Donovan Rebbechi" <> skrev i meddelandet
    news:...
    > On 2005-05-22, Tony Johansson <> wrote:
    >> Hello!
    >>
    >> I have these two class definitions below. Now to my question.
    >> This text is from a book that I say must be wrong.

    >
    > Oh boy. Get another book. You don't want to do a deep copy assignment with
    > a
    > reference counted class. Deep copy is one ownership strategy, reference
    > counting
    > is another one. By the way, what book is this ?
    >
    > Cheers,
    > --
    > Donovan Rebbechi
    > http://pegasus.rutgers.edu/~elflord/
    Tony Johansson, May 22, 2005
    #3
  4. Tony Johansson wrote:
    > Programming with design pattern!


    Try not to top-post here, BTW.

    > "Donovan Rebbechi" <> skrev i meddelandet
    > news:...
    >> On 2005-05-22, Tony Johansson <> wrote:
    >>> [...]

    >>
    >> [...]
    Victor Bazarov, May 22, 2005
    #4
  5. "Tony Johansson" <> wrote in message
    news:ed2ke.25043$...

    > I mean it's a big difference if you do a deep copy which mean that in this
    > example the left side would get brand new Point object identical to the
    > right side.


    Can you show us an example of a program that can detect the difference?

    For example, a program that works like this:

    Point p1;
    Point p2 = p1;

    // Do some stuff here, and then...

    if (...)
    std::cout << "Shallow copy\n";
    else
    std::cout << "Deep copy\n";

    What would you put in place of the ... to make this program work as
    suggested?
    Andrew Koenig, May 23, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ash
    Replies:
    1
    Views:
    400
    SenderX
    Oct 24, 2003
  2. Kalle Rutanen

    Reference counting in C++

    Kalle Rutanen, May 7, 2005, in forum: C++
    Replies:
    0
    Views:
    553
    Kalle Rutanen
    May 7, 2005
  3. Anne Wilson

    reference counting and PyTuple_SetItem

    Anne Wilson, Jun 10, 2004, in forum: Python
    Replies:
    0
    Views:
    649
    Anne Wilson
    Jun 10, 2004
  4. mathieu
    Replies:
    8
    Views:
    496
    Juha Nieminen
    Aug 31, 2008
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    183
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page