Skip to main content
GameDev.net gamedev.net
🔒 Locked

Hide or overwrite

Started by Ellal Nov 22, 2001 at 10:22 AM 1 replies 300+ views
Original Post
Ellal
Ellal
What''s the difference between overwriting a base-class method(in a derived class), and hiding it? Elall "It''''s all about reading between the lines"
"It''s all about reading between the lines"
Fruny
Fruny
In C++ you declare that you want to override a method with the 'virtual' keyword. That means that, instead of calling the method that is in the parent class, all methods must call the method appropriate to the class of the object.

Here is an example :

    

class Base
{
void Hidden()
{
cout << "Base::Hidden." << endl;
}
virtual void Overriden()
{
cout << "Base::Overriden." << endl;
}
void DoStuff()
{
Hidden();
Overriden();
}
}

class Child : public Base
{
void Hidden()
{
cout << "Child::Hidden." << endl;
}

void Overriden() // already virtual from Base

{
cout << "Child::Overriden." << endl;
}

void DoMoreStuff()
{
Hidden();
Overriden();
}
}

.
.
.

Base B;
Child C;

B.DoStuff()
// prints out

// Base::Hidden

// Base::Overriden


C.DoStuff()
// prints out

// Base::Hidden

// Child::Overriden


C.DoMoreStuff()
// prints out

// Child::Hidden

// Child::Overriden



Edit: Fixed the typo the next post points at.

Edited by - Fruny on November 22, 2001 3:42:52 PM
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.