assignment operator with smart pointer Data member

Assignment operator and copy constructor with smart pointer

Page 1 of 1

1 Replies - 2550 Views - Last Post: 02 September 2006 - 12:25 PM Rate Topic: -----

#1 bisinnoc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-September 06

assignment operator with smart pointer Data member

Post icon  Posted 02 September 2006 - 12:15 PM

#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

Is This A Good Question/Topic? 0
  • +

Replies To: assignment operator with smart pointer Data member

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: assignment operator with smart pointer Data member

Posted 02 September 2006 - 12:25 PM

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.dreami...wtopic13117.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1