Click here to Skip to main content
15,917,951 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Assertion failure in wincore.cpp Pin
Like2Byte24-Jan-07 7:40
Like2Byte24-Jan-07 7:40 
QuestionQue. about Class Constructor & Operator Overloading Pin
Andy Rama24-Jan-07 5:25
Andy Rama24-Jan-07 5:25 
AnswerRe: Que. about Class Constructor & Operator Overloading Pin
Waldermort24-Jan-07 5:35
Waldermort24-Jan-07 5:35 
GeneralRe: Que. about Class Constructor & Operator Overloading Pin
toxcct24-Jan-07 6:49
toxcct24-Jan-07 6:49 
GeneralRe: Que. about Class Constructor & Operator Overloading Pin
Waldermort24-Jan-07 8:24
Waldermort24-Jan-07 8:24 
AnswerRe: Que. about Class Constructor & Operator Overloading Pin
Roger Stoltz24-Jan-07 5:49
Roger Stoltz24-Jan-07 5:49 
GeneralRe: Que. about Class Constructor & Operator Overloading Pin
Andy Rama25-Jan-07 21:19
Andy Rama25-Jan-07 21:19 
GeneralRe: Que. about Class Constructor & Operator Overloading Pin
Roger Stoltz25-Jan-07 22:53
Roger Stoltz25-Jan-07 22:53 
Aniket Salunkhe wrote:
Roger Stoltz wrote:

The assignment operator declaration should look like this:
String& operator=( const char* str ); // For assigning a string, and...
String& operator=( const String& str ); // For assigning another object


why like this?


Which part is unclear?
This is how the assignment operator should be declared to work as expected.

Regarding your destructor I see that you're mixing memory management routines from both C and C++. Don't ever do that! In C++ you shall use new and delete.

Regarding your Trim function.
A Trim function is expected to trim the object it contains, not returning a trimmed copy of the original object. Personally I would like such a function to return a reference to itself in order to allow the use of cascading, e.g. str.TrimLeft().TrimRight() where TrimRight is called on the reference to the str object returned by TrimLeft.
In order to accomplish this your function should look like this:
String& String::Trim()
{
    // Here you trim m_string
    ....
    return *this;  // Returns a reference to the object itself, not a copy
}

I suspect that the runtime error you get when calling Trim is due to the abscense of a proper copy constructor. This error will disappear if you rewrite the Trim function as suggested above, but that doesn't mean you shouldn't write a copy constructor; you most certainly should in order to avoid shallow copies.

When you write a class that dynamically allocates memory you should always define at least:
- a default constructor that initializes the internal pointers
- a destructor that deallocates memory
- a copy constructor that make deep copies
- an overloaded assignment operator



"It's supposed to be hard, otherwise anybody could do it!" - selfquote

QuestionRe: Que. about Class Constructor & Operator Overloading Pin
Andy Rama26-Jan-07 6:17
Andy Rama26-Jan-07 6:17 
AnswerRe: Que. about Class Constructor & Operator Overloading Pin
Roger Stoltz26-Jan-07 12:02
Roger Stoltz26-Jan-07 12:02 
QuestionNeed help in getting Fax APIs Pin
Member 358569924-Jan-07 4:21
Member 358569924-Jan-07 4:21 
AnswerRe: Need help in getting Fax APIs Pin
Waldermort24-Jan-07 4:46
Waldermort24-Jan-07 4:46 
GeneralAnonymous seem to be back Pin
toxcct24-Jan-07 6:50
toxcct24-Jan-07 6:50 
AnswerRe: Need help in getting Fax APIs Pin
Hamid_RT25-Jan-07 19:23
Hamid_RT25-Jan-07 19:23 
QuestionDirectX and Circles Pin
Waldermort24-Jan-07 4:18
Waldermort24-Jan-07 4:18 
AnswerRe: DirectX and Circles Pin
Reagan Conservative24-Jan-07 6:07
Reagan Conservative24-Jan-07 6:07 
GeneralRe: DirectX and Circles Pin
Waldermort24-Jan-07 6:22
Waldermort24-Jan-07 6:22 
QuestionTrouble with Vista, MAPI, and Outlook 2000 Pin
gageller24-Jan-07 4:01
gageller24-Jan-07 4:01 
QuestionProblem in Creating Activex in AtlComponent Pin
narayanagvs24-Jan-07 3:13
narayanagvs24-Jan-07 3:13 
QuestionDetect ethernet interface STATUS?! Pin
djmarki24-Jan-07 2:57
djmarki24-Jan-07 2:57 
QuestionRe: Detect ethernet interface STATUS?! Pin
David Crow24-Jan-07 5:01
David Crow24-Jan-07 5:01 
QuestionGDI Text Input Control Required Urgently.. Pin
zeemalik24-Jan-07 2:50
zeemalik24-Jan-07 2:50 
AnswerRe: GDI Text Input Control Required Urgently.. Pin
Mark Salsbery24-Jan-07 5:39
Mark Salsbery24-Jan-07 5:39 
Questionbased pointer crashed my app Pin
Alex Cutovoi24-Jan-07 2:09
Alex Cutovoi24-Jan-07 2:09 
AnswerRe: based pointer crashed my app Pin
KarstenK24-Jan-07 2:27
mveKarstenK24-Jan-07 2:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.