Overloading problem

Pages: 12
An example of corrupting data from the OP's class using the + operator is:

1
2
TimpObject.h += TimpObject0.h;
TimpObject.m += TimpObject0.h; //user c&p this line but doesnt modify second argument, common error compiler doesnt catch should be TimpObject.m += TimpObject0.m;  


We started talking about classes in general, you use the methods im speaking of because later on the OP's Timp class might change to have a object member of a diffrent type, say a string that also holds the date, now he must modify all code written using his class instead of just re-writting a piece of the class. And if it creates an error in his code which compiler doesnt catch, then he has to look at his code and the classes to find error.

So you write class headers like im saying to avoid that. I think it is you that doesnt understand the quote from the book my friend... It makes no sense to have different methods of writing a class dependent on the member types.

Last edited on
I did not understand what you posted from the C++ Primer because what you posted and what you are writing have nothing common. It is obvious that you did not understand what is written in the book. There is nothing said that you should not write opertaor + and operator << in the book. All the C++ standard library is full of these operators. For example class std:: string has both operators and operator << and operator +. And neither class has such an awful function as suggested by you function addAllMembersFrom.
Last edited on
Ok ill walk you thru it.

Q: What happens if I add a member to his class called "date" that has a type string and I have allowed the user to use the + operator?

A: Anywhere my code uses:

1
2
Timp object0, object1;
cout << (object0 + object1);


Or similar, needs to be rewritten right? Now user must change class code and all code that used + operator...

Last edited on
If I use member function to modify members like combine, I dont change anything, class works as I wrote it and I am free to implement the member in the class however I want. So which would you rather do in a large class?


And again if typing a member function like object.addToObjectFrom(object2) isnt clear that it adds a object to the object your calling it on ,when it pops up saying Timp& addToObjectFrom(Timp& objectToAddFrom) in your IDE, then you must have a awful time using any function written by anyone but yourself as I am pretty sure I cant get more descriptive with a function name..
Last edited on
@pata

Q: What happens if I add a member to his class called "date" that has a type string ...

A. You will get another class. You had class Time and now you have class DateTime. They are different classes with different symantic.

You can include class Time as a member of class DateTime and use all operators of class Time in the realization of class DateTime. You need to change nothing in class Time itself.

A: Anywhere my code uses:

Timp object0, object1;
cout << (object0 + object1);


Or similar, needs to be rewritten right? Now user must change class code and all code that used + operator...

No, your code need not to be rewritten You simply need to write interface for class DateTime. One more Time and DateTime are different classes with different symantic.

If you are speaking about that you had class Date and added to it some member of type string then there is no difference whether you will rewrite operator + that allow to display the new member or you will rewrite some your function with strange name and will use instead of crystal clear statement

1
2
3
Date birthDate;

std::cout << birthDate << std::endl;

some much less clear code

1
2
3
Date birthDate;

Display( std::cout, birthDate );



The first code snip is well-known for everybody. You are using

std::string s;
std::cout << s;

the same way as

Date birthDate;
std:;cout << birthDate;



@pata

If I use member function to modify members like combine, I dont change anything, class works as I wrote it and I am free to implement the member in the class however I want. So which would you rather do in a large class?


There is no difference whether to use some function or an operator. In either case for the user interface is not changed. And in both cases you have to change realization of the function or of the operator. By the way operator << usually does not change members of the class for which it is written.
Moreover you always forgot that you need operation that does not change the object itself. I already showed that operator += and operator + have different symantic.
And I do not understand why you are not free to implement the operator <<. As I said operator << is a part of the interface of a class.
PLEASE don't argue in this thread. Use email then tell the OP what his answer is. Sorry if I seem rude. I just don't like bickering. :)
Thanks.
@Superdude


Is it your thread? I advice to pass right along that I would never hear about you.
PLEASE don't argue in this thread. Use email then tell the OP what his answer is.

Um, no. Discussions like this are exactly what the forum is for. They're useful and informative for other people learning C++, and other experts can weigh in with their own advice and experience. Keeping these discussions public benefits both the participants and everybody else.

Obviously, it's better if the tone can be kept civil and friendly.

I just don't like bickering. :)

So don't read it. Asking everybody else to change the way they use the forum to match what you personally do or don't like is pretty damn selfish.

@MikeyBoy
I just don't like bickering. :)


So don't read it. Asking everybody else to change the way they use the forum to match what you personally do or don't like is pretty damn selfish.


I totally agree with you, MikeyBoy. I could understand this person if he took part in the disscusion of the thread early. But he did not. So I do not understand why he appeared here.
Sorry if you feel that way. I tried to search for a answer to a problem I had and looked here. I read the OP's question, and then read a two page discussion about stuff that was not completly relevant. He just wanted to know what his problem was.

They're useful and informative for other people learning C++, and other experts can weigh in with their own advice and experience.

There have only been two people talking in this thread. Also, I don't understand half of what you are saying, and don't see how much of it is relevant. I just wanted an answer to the OP's Q. What I got is a two page discussion over something else.

I don't mean to insult you or start a fight. I was just trying to keep the thread on course.
Sorry if you feel that way. I tried to search for a answer to a problem I had and looked here. I read the OP's question, and then read a two page discussion about stuff that was not completly relevant. He just wanted to know what his problem was.

And the OP got his answer right away, in the very first response. Both he and, apparently, you, got what you came here for.

Somebody else then weighed in with some "advice" that is unhelpful and misleading, based on a misunderstanding of the principles of OOP. Vlad is trying to help both this person, and any other beginners who are reading, to correct this misunderstanding and come to a better understanding of the principles of encapsulation and OOP.

It is entirely right and proper that those of us with experience try and explain this sort of thing. Otherwise, how will people who come here looking to learn C++ and deepen their understanding ever going to achieve that if they find a bunch of half-truths and poor advice? Do you honestly think that would make this forum a better place?

I don't understand half of what you are saying, and don't see how much of it is relevant. I just wanted an answer to the OP's Q. What I got is a two page discussion over something else.

The answer was given. The thread evolved - as threads on forums do. If there's something you don't understand, then by all means ask. That's what this forum is for, after all. But it's not your place to try and quash discussion that you personally aren't interested in, and it's incredibly rude of you to do so.

Last edited on
@pata:
Vlad is right. Case is closed.
You just needed to put a "const" right there, nomore, noless.
There is no memory corruption, safety issues, crashes deriving from that.
You didn't really need to write all this poem, you just needed to test it in the first place to understand it WORKS.

It's the first time I see a low-postcount user argue with a high-postcount user as vlad saying he's wrong.
-- Edit --
Now I may be partially wrong because I see many many off-topic posts.
But that's the point of the main question.

I really cannot understand clearly what could possibly be the other questions.
Last edited on
To be fair to pata, there's nothing wrong with asking further questions based on something another user has posted, if there's something you don't understand, or if there's something that clashes with your own understanding. After all, that's the whole point of this forum - for us all to learn from each other.

We deepen our understanding of a topic by discussing it, and by learning from one another.

Of course, it helps if the tone of the discussion is kept friendly and civil. Adopting a belligerent, hectoring tone helps no-one. But nor does trying to stifle discussion and dictate to people that they shouldn't discuss a topic.
It's good to ask questions.
But, still
EssGeEich wrote:
I really cannot understand clearly what could possibly be the other questions.


The other questions are really written in a poor way.
(Even some answers, vlad, lol)


----TLDR----
Write your questions in a cleaner way.
Ensure your grammar is correct.
Check the correctness of the answers given.
Try to play with some code before asking.
I'm sorry. I didn't mean it that way. I can just leave this discussion if you want me to. I just didn't care for the long discussion once the case was closed. Sorry if I made you mad. Really.
Topic archived. No new replies allowed.
Pages: 12