We had a base class that had a member function that incorrectly returned a pointer instead of a const pointer.
While looking at this, I came across some behavior I didn't expect. The following code snippets illustrate.
This code snippet fails to compile because Y is abstract:
Code:
#include <iostream>
using namespace std;
class X
{
public:
virtual const char* getName() const=0;
};
class Y : public X
{
public:
virtual char* getName() {
static char name[] = "bob";
return name;
}
};
int main()
{
Y y;
cout << y.getName() << endl;
return 0;
}
That makes sense to me: "virtual const char* getName() const" is never defined.
This fails to compile because of conflicting definition of the return type:
Code:
using namespace std;
class X
{
public:
virtual const char* getName() const=0;
};
class Y : public X
{
public:
virtual char* getName() const {
static char name[] = "bob";
return name;
}
};
int main()
{
Y y;
cout << y.getName() << endl;
return 0;
}
Why does the difference between the two (constness of the function) result in the two different errors?