Advertisement

Copy Constructor

Started by May 13, 2002 06:29 PM
15 comments, last by Tac-Tics 22 years, 9 months ago
Let me add this quickly.

I think the assignment operator should return a reference to the object (return *this and should check for self assignment (if( &rhs != this ) ).

Also, the parameter should be a const reference (const String& s).
  String& operator=( const String& s ){    if( &s != this )    {        // do stuff    }    return *this;}  


----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com

[edited by - Blaster on May 14, 2002 4:04:42 PM]
quote:
Original post by Michalson
The primary reason your post was edited was due to your use of the word "stupid" in decribing both the original poster and his question.

Despite you asking me not to post on this again, I feel it is entirely appropriate I do so, so that I may refute this in the most applicable context. I did *not* describe the OP as stupid, and I did not describe his question as stupid. What I did feel was very wrong was the confidently stated assertion: "The reason I''m writing my own string class is because I don''t particularly like using C-style strings and I do not particularly like the standard library". I gave a rebuke I felt was appropriate to the folly being demonstrated.

quote:
Original post by Tac-Tics
I challenge you to find anything suggesting the experience of creating such a class is counter-productive in any way.

It is self-evident that writing something which already exists is counter-productive. Have you any idea of the amount of design and testing effort that the world''s leading C++ developers have put into developing std::string? Note that I''m responding entirely to the reasons you gave, that you "do not particularly like the standard library". There *are* reasons to write a string class, but that''s one of the least convincing.
Advertisement
>> Anon
Second, there is a minor logical error in the source presented by Tac-Tics. Since ''charList'' is a pointer to an array, it must always be deleted using brackets like so: ''delete [] charList;''. This is the only way that the compiler knows that there is a whole array of chars to be deleted as opposed to just a single one.

I read in one of my books (C++ in Plain English), that most compilers do not distinguish between delete and delete[] and I simply took that to mean all compilers follow that rule. However, I will note that in the future.

>> Anon again
A final note, Tac-Tics is to be commended for taking the time to implement their own string class. The best way to learn about any data structure, especially its limitations and when it is appropriate to use, is to implement that data structure at least once. This is the best way to learn programming.

At least someone has a positive attitude =-)

>> Blaster
I think the assignment operator should return a reference to the object (return *this ) and should check for self assignment (if( &rhs != this ) ).

Yeah, we did learn about checking for redundant copying, thnx for reminding me about that. Question, though. Why is it beneficial to have = return the object?

>> SabreMan (again)
It is self-evident that writing something which already exists is counter-productive. Have you any idea of the amount of design and testing effort that the world''s leading C++ developers have put into developing std::string? Note that I''m responding entirely to the reasons you gave, that you "do not particularly like the standard library". There *are* reasons to write a string class, but that''s one of the least convincing.

If it''s counter-productive, then may I inquire TO WHAT? I learned many new things creating the class. Surely that isn''t counter-productive. And the creators of the STD lib? My efforts did not affect them either. Did I reinvent the wheel? Yes. Was it neccessary? For something to be neccessary, there must first be a need. What did I need? I needed to learn more about class and I needed a better String class. I succeeded in both didn''t I? If that is counter-productive from your perspective, perhaps you could explain what you mean by the word. For a days work, I feel I learned a lot (and now I have a nice "friendlier" class to deal with).

Also, it doesn''t concern me how much effort the worlds best C++ programmers put into the STD lib. I prefer the Java standard library over their efforts any day. All their work is neatly documented uniformly (read: javadoc.exe) on their webpage for easy access and the classes themselves are, at least IMHO, a hellalot easier to use. (not to mention it is MUCH more comprehensive than the STD lib). Unfortunately, since I''m working with C++ here and not Java, I just had to improvise (ie: recreate it myself).

(PS: forgive me for asking so many rhetorical questions above ^_^ )

"You TK''ed my chicken!"
quote:
Original post by Tac-Tics
If it''s counter-productive, then may I inquire TO WHAT? I learned many new things creating the class. Surely that isn''t counter-productive.

Sure, that''s all well and good. I''m not after an argument here, I just want to make sure you don''t really avoid the Standard Library just because you "don''t like it". It seems you said one thing and meant another - in which case you can''t blame me for not knowing why you were writing a string class.
I''ve asked once nicely, and since you can obviously read I''ll ask nicely for the last time: Please keep your discussion over being muted out of this thread, there will be a thread where you can discuss any problems you have with me in less than 30 minutes. For now this is Tac-Tics''s thread, so if it doesn''t apply to his question then don''t say it.
Tic-Tacs :
I am sorry if I was redundant about the self assignment, my mistake. I did try to make a quick reply... maybe too quick.

About the return, I think it's simply how the operator should work. But to be more precise, look at this :
if( ( p = fopen( "foo.bar", "r" ) ) == NULL ) 

Yeah I know, it doesn't look really good and it's hard to read, but it's valid. The assignment to 'p' returns a reference to 'p' that can be compared to NULL.
I guess this is valid also :
if( ( s = "the string" ) == "not the string" ) 


----------------
Blaster
Computer game programmer and part time human being
Strategy First - http://www.strategyfirst.com
BlasterSoft - http://www.blastersoft.com

[edited by - Blaster on May 14, 2002 4:55:47 PM]
Advertisement
Woah! I missed this paragraph in my previous response...

quote:
Original post by Tac-Tics
Also, it doesn''t concern me how much effort the worlds best C++ programmers put into the STD lib. I prefer the Java standard library over their efforts any day. All their work is neatly documented uniformly (read: javadoc.exe) on their webpage for easy access and the classes themselves are, at least IMHO, a hellalot easier to use. (not to mention it is MUCH more comprehensive than the STD lib). Unfortunately, since I''m working with C++ here and not Java, I just had to improvise (ie: recreate it myself).


This is getting into religious territory, so I''ll try and restrict myself to objective comments only. Firstly, your preference for the Java standard library does not mean it is better - this is a subjective measure. For example, I personally prefer the C++ Standard Library. Secondly, all the work of the C++ Standard Committee is also neatly documented uniformly - the only drawback being you have to pay for it. Java is a proprietary commercial effort - it makes sense for Sun to make documentation freely available, but the C++ guys have to make money somehow. Josuttis'' book is at least as good (IMO) as the Sun Java docs.

Thirdly, saying that the Java libraries are more comprehensive than the Java libs betrays a misunderstanding of the design criteria of both. There are certain libraries not suitable for inclusion into the C++ Standard, but that doesn''t mean they''re not available should you require them. Lastly, you''ve reverted to implying you had to write a string class from necessity. Can you explain what is so difficult about std::string that you had to create a "simpler" version? What can you do with your class that is "too difficult" with std::string?

This topic is closed to new replies.

Advertisement