Calling Base Class Operators

This is a discussion on Calling Base Class Operators within the C++ Programming forums, part of the General Programming Boards category; I have a question about calling operators of a base class from a derived class. Here's some sample code: Code: ...

  1. #1
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399

    Calling Base Class Operators

    I have a question about calling operators of a base class from a derived class. Here's some sample code:
    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    class Base
    {
    public:
    	Base(int a) : m_a(a) {}
    	bool operator==(const Base& op1)
    		{
    			return (m_a == op1.m_a);
    		}
    private:
    	int m_a;
    };
    
    class Derived : public Base
    {
    public:
    	Derived(int a, int b) : Base(a), m_b(b) {}
    	bool operator==(const Derived& op1)
    		{
    			return (((Base*)this)->operator==(op1)) && 
    				(m_b == op1.m_b);
    		}
    private:
    	int m_b;
    };
    
    int main()
    {
    	Derived a(10,3), b(10,3);
    
    	if (a == b)
    		cout << "Equal" << endl;
    	else
    		cout << "Not equal" << endl;
    
    	return 0;
    }
    The bold part is the part I'm wondering about. I know I can do it that way and it seems to work, but is there a prettier way to do it? I realize that I could have m_a be protected instead of private and then test, but m_a needs to be private. I was hoping for some sort of syntax that didn't have operator== explicitly stated and that looked more like a normal == test.

    Thanks.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    How about:

    Code:
        bool operator==(const Derived& op1) {
             return ( static_cast<Base>(*this) == static_cast<Base>(op1)) && (m_b == op1.m_b);
        }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Quote Originally Posted by pianorain
    I know I can do it that way and it seems to work, but is there a prettier way to do it?
    Maybe:

    Code:
    return m_b == op1.m_b && Base::operator==(op1);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. virtual base class constructor
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 03-24-2008, 12:43 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21