Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,392 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,120 people online right now. Registration is fast and FREE... Join Now!




assignment operator with smart pointer Data member

 
Reply to this topicStart new topic

assignment operator with smart pointer Data member, Assignment operator and copy constructor with smart pointer

bisinnoc
2 Sep, 2006 - 11:15 AM
Post #1

New D.I.C Head
*

Joined: 2 Sep, 2006
Posts: 1


My Contributions
CODE

#ifndef ADDRESS_BOOK_H
#define ADDRESS_BOOK_H
// address_book.h
// class Image and AddressBookEntry declarations

#include <iostream>
#include <string>
//#include <algorithm>
using namespace std;

#include <boost/smart_ptr.hpp>
using namespace boost;

#include "Person.h"

//! A dummy class for image data.
class Image
{            
public:
    Image(const string& image_data_filename):
        _image_filename(image_data_filename)
        {
            // read image file from disk ...
        };
    string name() {return _image_filename;};
    void name(string newname) { _image_filename = newname; }

private:
    string _image_filename;
    // data member to store image ...
};

// -------------------------------------------------------
class AddressBookEntry
{  
public:

          //constructor for AddressBookEntry    
          AddressBookEntry(const Person& person, const string& image_filename = "");

         // copy constructor for the AddressBookEntry
    AddressBookEntry(const AddressBookEntry& rhs)
         //Assignment operator
               AddressBookEntry& operator =(const AddressBookEntry& rhs)
         void mySwap(AddressBookEntry & temp)



    string getImageName() const;
    void setImageName(string newname);
    Person getPerson() const { return _person; }
    
private:
    Person  _person
      scoped_ptr<Image>  _image_ptr
};
#endif

//Attempt to implement copy constructor & assignmnent operator

AddressBook::AddressBookEntry(const AddressBookEntry& rhs):
  
      // this is where trouble comes in !!!
      _person(rhs.getPerson()),
     _image_pt(rhs.getImageName());
{ }

AddressBook::operator=(const AddressBookEntry& rhs):
{
     //  create temp for rhs  to ensure atomic operation
     AddressBookEntry temp(rhs);
    
      //swap temp with the object being assigned
     mySwap(temp);
     //return the object being assigned
    return *this;
    
}

// here is my  :angry: swap function using the swap from <algorithm >
void AddressBookEntry::mySwap(AddressBookEntry &  temp)
{
//   swap each data member for the two object

   _person.swap(temp.getPerson());
    //another trouble spot here !!
   _image_ptr(temp.getImageName());
  
}


edit: added [code] tags, moved to C/C++ ~ jayman9
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Assignment Operator With Smart Pointer Data Member
2 Sep, 2006 - 11:25 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,985



Thanked: 45 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Do you have a question or problem with your code?

Please review the Forum rules concerning posting to make sure that you have provided all the relavent information our members need in order to help you.

The Forum rules can be found at the following link.

http://forums.dreamincode.net/showtopic13117.htm
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 02:37AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month