1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Casting problem

Discussion in 'C++' started by Sinaru, Aug 31, 2010.

  1. Sinaru

    Sinaru New Member

    Aug 31, 2010
    Likes Received:
    Trophy Points:
    I have two classes and one of them is inherited from the other class.
    Class Person - used to store first name and last name of a person. The data members are protected.
    Class Candidate - used to store votes of an election, and this is inherited from Person class so that I can store name of the candiadate.

    I have overloaded these operators in Person class : ==, !=, =

    I have also overloaded the same operators in Candidate class too but the definision are same because you can only use the name of the candidate to compare. So rather than copy and paste the same code, I used static_cast. It worked but I wonder is it bad to do like that?

     bool Candidate::operator ==(const Candidate& right) const
        //return (firstName == right.firstName && lastName == right.lastName);
        // Rather than the above, I used this...
        return (static_cast(*this) == static_cast(right));

Share This Page