#include
#include
/**
* There're some serious issues in class Base and Subclass.
* You should check for current and POTENTIAL bugs in them,
* especially memory problems.
*/
class Base
{
private:
char * name;
public:
Base(char * className)
{
name = new char[strlen(className)];
strcpy(name, className);
}
~Base()
{
delete name;
}
char * copyName()
{
char newname [256];
strcpy(newname, name);
return newname;
}
virtual char * getName()
{
return name;
}
static void print(Base base)
{
printf("name: %s\n" , base.name);
}
};
class Subclass : public Base
{
public:
Subclass(char * className) : Base(className)
{
}
};
int main()
{
Base * pBase = new Subclass("test");
Base::print(*pBase);
printf("name: %s\n", pBase->getName());
printf("new name: %s\n", pBase->copyName());
return 0;
}
--------------------next---------------------
阅读(1705) | 评论(0) | 转发(0) |