C
cheeser
Hello all,
Please see the question in the code below...
Thanks!
Dave
#include <iostream>
using namespace std;
class foo
{
public:
foo(int d) : data(d) {}
void print() const {cout << "const" << endl;}
void print() {cout << "non-const" << endl;}
private:
int data;
};
int main()
{
foo a(42);
// The call below calls the non-const print();
// I want the const print() to be called!
// a.print();
// Here's one way to do it. Is there a more elegant way?
// Making a const is not within the scope of my question!
static_cast<const foo>(a).print();
return 0;
}
Please see the question in the code below...
Thanks!
Dave
#include <iostream>
using namespace std;
class foo
{
public:
foo(int d) : data(d) {}
void print() const {cout << "const" << endl;}
void print() {cout << "non-const" << endl;}
private:
int data;
};
int main()
{
foo a(42);
// The call below calls the non-const print();
// I want the const print() to be called!
// a.print();
// Here's one way to do it. Is there a more elegant way?
// Making a const is not within the scope of my question!
static_cast<const foo>(a).print();
return 0;
}