Skip to content

Instantly share code, notes, and snippets.

@MihailJP
Created December 7, 2012 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MihailJP/4236262 to your computer and use it in GitHub Desktop.
Save MihailJP/4236262 to your computer and use it in GitHub Desktop.
You cannot override a class (static) constant
#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;
}
#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