I would like to have an object accessible across all my application classes. So I though:
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(){}
int id(){return -1;}
};
extern const MyClass myClass;
int main() {
cout << myClass.id();
return 0;
}
And anywhere I need it, I'll do:
extern const MyClass myClass;
and just use it, like:
cout << myClass.id();
But, I was wrong. That returns error:
error: passing 'const MyClass' as 'this' argument of 'int MyClass::id()' discards qualifiers [-fpermissive]
I Guess I could do static MyClass myClass;, instead. And so I will have more or less the same functionality.
What's the best/correct approach?
Aucun commentaire:
Enregistrer un commentaire