In the code below, the function getName()
returns a char *
. I would of thought that it should (it also can) return a string
. How does cout
correctly print it to the console if it is just a pointer to the first char?
#include <iostream>
#include <string>
using namespace std;
class Base
{
protected:
int m_value;
public:
Base(int value)
: m_value(value)
{
}
const char* getName() { return "Base"; }
//string getName() { return "Base"; }
int getValue() { return m_value; }
};
int main()
{
Base base(5);
std::cout << "Base is a " << base.getName() << " and has value " << base.getValue() << '\n';
return 0;
}