derived.cpp
#include "Base.h"
class Derived : public Base
{
public:
void did(){cout << "this is derived class" << endl;
cout << "tt = " << getTT() << endl;}
private:
};
base.cpp
#include <iostream.h>
class Base
{
public:
int getTT() { return tt;}
void did(){};
void done();
private:
int tt;
};
int main()
{
Base b;
b.done();
return 0;
}
void Base::done()
{
tt = 8;
Base* b=new Derived; // ?????????????????
b->did();
delete b;
}
问题:
我想在base class内部,定义一个base pointer, 用derived class初始化,同时又让他是这个base class的derived class, 也就是说,初始化完成后,b->did()应该能够能够打印出
this is derived class
tt=8
Base* b=new Derived; //???????????????????????]
这一句有问题,谁知道应该怎样初始化?
在base class外面好办,在里面怎么办?行得通吗?
谢谢
#include "Base.h"
class Derived : public Base
{
public:
void did(){cout << "this is derived class" << endl;
cout << "tt = " << getTT() << endl;}
private:
};
base.cpp
#include <iostream.h>
class Base
{
public:
int getTT() { return tt;}
void did(){};
void done();
private:
int tt;
};
int main()
{
Base b;
b.done();
return 0;
}
void Base::done()
{
tt = 8;
Base* b=new Derived; // ?????????????????
b->did();
delete b;
}
问题:
我想在base class内部,定义一个base pointer, 用derived class初始化,同时又让他是这个base class的derived class, 也就是说,初始化完成后,b->did()应该能够能够打印出
this is derived class
tt=8
Base* b=new Derived; //???????????????????????]
这一句有问题,谁知道应该怎样初始化?
在base class外面好办,在里面怎么办?行得通吗?
谢谢