Class 指向C++类派生类的指针
我在派生指向派生类的指针时遇到了一些问题。我认为这与构造函数有关。我必须在派生类中创建新的构造函数吗?如何从派生类创建指针?谢谢Class 指向C++类派生类的指针,class,pointers,derived,Class,Pointers,Derived,我在派生指向派生类的指针时遇到了一些问题。我认为这与构造函数有关。我必须在派生类中创建新的构造函数吗?如何从派生类创建指针?谢谢 Class Base { public: int myfunction(int a,int b,int c) { return a+b+c; } }; Class Derived: public Base { int newfunction(int a, int b, int c) { return a*b*
Class Base
{
public:
int myfunction(int a,int b,int c)
{
return a+b+c;
}
};
Class Derived: public Base
{
int newfunction(int a, int b, int c)
{
return a*b*c;
};
};
int main()
{
// this doesn't work at all.. I get all errors every time I try to refer to the object
//instantiated from my derived class.
//I know it's my lack of understanding.
Derived *NewObject = new Derived;
//Why wont this work?
}
C++区分大小写,关键字类是小写的。您为这两个类都编写了类,这似乎是代码中唯一的问题。这只是示例中的一个输入错误。我不能粘贴实际的代码,因为我实际上是从一些开源软件FLTK派生一个类,而且我似乎不能从中声明一个新类。可能代码太多,会阻碍进程。你能用基类做到这一点吗?@user3523966:我打赌问题在于分配对象,而不是将其分配给指针。只是新的派生工作吗?如果没有,那么你应该给我们更多的信息,就像你的编译器显示的错误一样。