Solved

Operator overloading required

Posted on 2003-11-03
9
242 Views
Last Modified: 2013-11-20
I have a class that I derived from CObject...and the strangest thing is happening. There are no dynamic members...yet the compiler insists that I must overload an operator for any operation I want to do with it. Usually....the compiler defaults to a member-by-member copy constructor...however..this one does not. What is different about this class??

class CUserProfile : public CObject
{
      DECLARE_SERIAL(CUserProfile)

public:
      CUserProfile();
      CUserProfile(CUserProfile* cp);
      virtual ~CUserProfile();
      virtual void Serialize(CArchive& ar);

private:
      //User Profile
      CString m_sFullName;
      CString m_sEMailAddress;
      CString m_sNick;
      CString m_sAltNick;
      CString m_sPassword;
      bool m_bInvisible;
      bool m_bUsePassword;

      //Connection Options
      int m_nRetrys;

      //Display Options
      bool m_bPartJoin;
      bool m_bQuit;
      bool m_bColors;
      bool m_bShowUnknowns;

      //DCC Options
      int m_nPacketSize;
      bool m_bFillFileSpaces;
      bool m_bFastSend;
      bool m_bResume;
      bool m_bWarnAccept;
      CString m_sGetFolder;
      CString m_sSharedFolder;
public:
      bool SetNick(CString n);
      bool SetAlternateNick(CString an);
      void SetRealName(CString rn);
      void SetEmail(CString em);
      void SetPassword(CString p);
      void SetInvisible(bool i);
      void SetRetryCount(int rc);
      void SetShowJoinPart(bool jp);
      void SetShowUnknownMsgs(bool um);
      void SetShowColors(bool sc);
      void SetShowQuits(bool sq);
      void SetPacketSize(int ps);
      void SetFillSpaces(bool fs);
      void SetFastSend(bool fs);
      void SetResumeOnExist(bool roe);
      void SetWarnOnAccept(bool woa);
      void SetGetFolder(CString gf);
      CString GetNick(void);
      CString GetAltNick(void);
      CString GetRealName(void);
      CString GetEmail(void);
      bool GetInvisibleMode(void);
      CString GetPassword(void);
      int GetRetryCount(void);
      bool GetShowJoinPart(void);
      bool GetShowUnknown(void);
      bool GetShowColors(void);
      bool GetShowQuits(void);
      int GetPacketSize(void);
      bool GetFillFileSpaces(void);
      bool GetFastSend(void);
      bool GetResumeOnExist(void);
      bool GetWarnOnAcceptSend(void);
      CString GetSendFolder(void);
      
      //CUserProfile operator=();
      bool GetUsePassword(void);
      void SetUsePassword(bool up);
};


0
Comment
Question by:SGyves
  • 4
  • 3
  • 2
9 Comments
 

Author Comment

by:SGyves
ID: 9675933
For example I can't say obj1 = obj2;
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 9675967
Your variables are all but POD, so you will need both a copy constructor and assignment operator:

CUserProfile::CUserProfile(const CUserProfile& r) {

    m_sFullName=r.m_sFullName ;
    m_sEMailAddress=r.sEMailAddress;

    // for all your members....
}


CUserProfile& CUserProfile::operator=(const CUserProfile& r) {

    m_sFullName=r.m_sFullName ;
    m_sEMailAddress=r.sEMailAddress;

    // for all your members....

    return *this;
}

And, you'll have to declare them like

    CUserProfile(const CUserProfile& r);
    CUserProfile& operator=(const CUserProfile& r);

in your class.


0
 
LVL 9

Expert Comment

by:Priyesh
ID: 9675989
This is from AFX.h

      // Disable the copy constructor and assignment by default so you will get
      //   compiler errors instead of unexpected behaviour if you pass objects
      //   by value or assign objects.
protected:
      CObject();
private:
      CObject(const CObject& objectSrc);              // no implementation
      void operator=(const CObject& objectSrc);       // no implementation


So this behaviour is by design.. :)

0
 
LVL 86

Expert Comment

by:jkr
ID: 9676057
>>So this behaviour is by design.. :)

You are right, but it does not say one can't override it :o)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:SGyves
ID: 9676110
Ok...just wondering why this object suddenly started making me overload assignment and copy. Thank you for the explanation.  :)
0
 
LVL 86

Expert Comment

by:jkr
ID: 9676142
>>just wondering why this object suddenly started making me overload assignment and cop

It became too complex for the compiler to handle - and, I actually would recommend implementing these *always*, as a matter of "taste" :o)
0
 

Author Comment

by:SGyves
ID: 9676631
Yes...yes...I know...it is proper practice to always overload the copy and assignment. Ahhh...so hard not to be lazy. I agree though.
0
 
LVL 9

Expert Comment

by:Priyesh
ID: 9676635
jkr,
I did not say you cannot override it either! SGyves was wondering why CObject would force him to write an overloaded = operator and tried to answer to the point.
0
 

Author Comment

by:SGyves
ID: 9676845
I understood Priyesh....no worries.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now