Yes the compiler let me do this but the result is that is called the print function from class pub,I tried and static_cast and I get the same result...
You can try:
Code:
#include <iostream>
class pub
{
public:
void print()
{
std::cout << "pub::print()" << std::endl;
}
};
class man : public pub
{
public:
void print()
{
std::cout << "man::print()" << std::endl;
}
};
int main()
{
pub* p1 = new man;
static_cast<man*>(p1)->print();
delete p1;
return 0;
}
But of course the better solution is:
Code:
#include <iostream>
class pub
{
public:
virtual void print()
{
std::cout << "pub::print()" << std::endl;
}
};
class man : public pub
{
public:
void print()
{
std::cout << "man::print()" << std::endl;
}
};
int main()
{
pub* p1 = new man;
p1->print();
delete p1;
return 0;
}
You might consider making print() const.