Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6
Next time, please provide a minimal example that shows the problem. No need for the wall of code. It'll also increase the chance of getting a useful reply Probably the easiest way to check if 2 classes are the same is to give the base class an additional variable called "ID" or something, preferably of an integral type, and initialize it to a value that is unique to each class. For example the Item class' constructor would initialize this variable to 0, class Food to 1 and class Tool to 2. Then as an additional check you simply do id == obj.id; // True if they are the same class, false otherwise. ( No need for this->, people usually use prefixes for member variables, like id => m_id ) At least, this is what I think you mean. :) Also, I recommend marking your Item's destructor as virtual. and in addition to AZTECCO's reply: https://isocpp.org/wiki/faq/const-correctness
24th May 2019, 2:08 PM
Dennis
Dennis - avatar
+ 3
bool operator ==(const Item& obj) { return ( this->name == obj.name && this->price == obj.price && this->weight == obj.weight ); }
24th May 2019, 1:57 PM
AZTECCO
AZTECCO - avatar
+ 1
Thank you Dennis for clarification about "const" ! And for this-> .. some people still use it for readability, I prefer to not use it too. The same applies to return (....); ()are not needed to make it work
24th May 2019, 3:08 PM
AZTECCO
AZTECCO - avatar
+ 1
AZTECCO Yeah, it's not that much of an issue if you use this-> AND a prefix. But only using this-> can cause annoying bugs when you inevitably forget it and 2 variable names match. Can't get that issue if you used a prefix.
24th May 2019, 3:50 PM
Dennis
Dennis - avatar
24th May 2019, 2:30 PM
Paul
0
Class classname : public derived classname
19th May 2020, 11:05 PM
Naveed
Naveed - avatar