Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help please!

Status
Not open for further replies.

FFCH

Programmer
Oct 17, 2006
10
AU
I am so lost its crazy, i have:

• Constructor: 2 argument
The 2-argument constructor takes a media and entertainment object as parameters. A copy should be made of the parameters through the createCopy function, if they are valid and stored in the current Entry object.

set up is as follows

Code:
Entry::Entry():_media(NULL),_entertainment(NULL){}

Entry::Entry(const Entry& entry):_media(NULL),_entertainment(NULL)
{
						
}

Entry::Entry(const Media& media, const Entertainment& entertainment)
{
	*_media = new Media();
	Entertainment *_entertainment = entertainment.createCopy();
	
	
}
Entry::~Entry()
{
	delete _media;
	delete _entertainment;
}
Media * Entry::getMedia()
{
	
	return _media;

}

const Media * Entry::getMedia() const
{
	
	return _media;

}

Entertainment * Entry::getEntertainment()
{

	return _entertainment;

}

const Entertainment * Entry::getEntertainment() const
{
	
	return _entertainment;

}

bool Entry::isValid() const
{
	
	return _media != NULL && _entertainment != NULL;


}

bool Entry::operator ==(const Entry& obj) const
{
	if (getEntertainment()->getEntertainmentType() != obj.getEntertainment()->getEntertainmentType())
	{
		return false;
	}
	if (getMedia()->getMediaType() != obj.getMedia()->getMediaType())
	{
		return false;
	}
	if (getEntertainment()->getTitle() != obj.getEntertainment()->getTitle())
	{
		return false;
	}
	
	return true;
}

bool Entry::operator !=(const Entry& entry) const
{
	return !(*this==entry);
}

Please help
 
Why not pass them by value and implement a copy constructor on those classes?

BTW, you didn't tell us what kind of problem you're having.
 
I dont know how to do that? That is my main problem
 
Code:
class Something
{
private:
   // Some member variables.
   int     m_Int;
   string  m_Str;
   char*   m_CStr;

public:
   Something();  // Default constructor.

   Something( const Something&  obj );  // Copy Constructor.

   Something& operator=( const Something&  rhs );  // Assignment operator.
};

Something::Something()
:  m_Int( 0 ),   // Initialization list.
   m_Str(),
   m_CStr( NULL )
{
   // Do any addition construction here.
}

Something::Something( const Something&  obj )
:  m_Int( obj.m_Int ),  // Initialization list.
   m_Str( obj.m_Str ),
   m_CStr( NULL )
{
   // m_CStr is a char*, so we have to allocate memory and
   // copy it outselves.
   if ( obj.m_CStr != NULL )
   {
      m_CStr = new char[ strlen( obj.CStr ) + 1 ];
      strcpy( m_CStr, obj.m_CStr );
   }
}

Something& Something::operator=( const Something&  rhs )
{
   // If right hand side is the same object as this, just return this,
   // otherwise bad things could happen.
   if ( this == &rhs )
   {
      return *this;
   }

   m_Int = obj.m_Int;
   m_Str = obj.m_Str;

   // Delete out memory first.
   if ( m_CStr != NULL )
   {
      delete [] m_CStr;
      m_CStr = NULL;
   }

   // Them copy the char* string from rhs.
   if ( obj.m_CStr != NULL )
   {
      m_CStr = new char[ strlen( obj.CStr ) + 1 ];
      strcpy( m_CStr, obj.m_CStr );
   }
}
 
Okay because i have objects like defined in the header file which is Entertainment *entertainment; and Media *_media; and have a virtual function create copy if i did the follwoing would be enough
Code:
Entry::Entry(const Entry& entry):_media(NULL),_entertainment(NULL)
{
	if(entry.isValid())
	{
		_media->createCopy();
		_entertainment->createCopy();
	}
}
 
Shouldn't you get a copy of the entry object being passed in? Also, since you aren't passing an Entertainment object in this constructor, how can you copy one?
Code:
Entry::Entry(const Entry& entry):_media(NULL),_entertainment(NULL)
{
    if(entry.isValid())
    {
        _media = entry->createCopy();
    }
}
 
yeah but create copy is not a member of entry
 
Oh crap, you caught me before I had my coffee. ;-)
Something like this should work:
Code:
Entry::Entry(const Entry& entry):_media(NULL),_entertainment(NULL)
{
    if(entry.isValid())
    {
        if ( entry._media != NULL )
        {
            _media = entry._media->createCopy();
        }

        if ( entry._entertainment != NULL )
        {
            _entertainment = entry._entertainment->createCopy();
        }
    }
}
 
i know what you mean i cant function with out it thanks for the help..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top