Created
December 7, 2012 20:28
-
-
Save MihailJP/4236262 to your computer and use it in GitHub Desktop.
You cannot override a class (static) constant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class Parent { | |
protected: | |
static const int val = 1; | |
public: | |
virtual int getval() {return val;} | |
}; | |
class Child : public Parent { | |
private: | |
static const int val = 2; // trying to override... | |
}; | |
int main() { | |
Child* instance = new Child(); | |
std::cout << instance->getval() << std::endl; // You'll see '1', expected '2'. | |
delete instance; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
class Parent { | |
protected: | |
virtual const int val() {return 1;} | |
public: | |
virtual int getval() {return val();} | |
}; | |
class Child : public Parent { | |
private: | |
const int val() {return 2;} // trying to override... | |
}; | |
int main() { | |
Child* instance = new Child(); | |
std::cout << instance->getval() << std::endl; // You'll see '2', as expected. | |
delete instance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment