未调用被重写的虚方法
Overriden Virtual methods not being called
我试图创建一个抽象类,其他一些类可以基于arduino项目。但是,每当我调用一个在基中是虚的方法时,它只会调用基实现。下面的代码。有人能看出我哪里做错了吗?
#define RTCBASE 0
class RTC_Base {
public:
virtual uint8_t begin(void){ return 0; };
virtual void adjust(const DateTime& dt){};
virtual DateTime now(){ return DateTime(); };
virtual int Type(){ return RTCBASE; };
};
////////////////////////////////////////////////////////////////////////////////
// RTC based on the DS1307 chip connected via I2C and the Wire library
#define DS1307 1
class RTC_DS1307 : public RTC_Base
{
public:
virtual int Type(){
return DS1307;
}
uint8_t begin(void);
void adjust(const DateTime& dt);
uint8_t isrunning(void);
DateTime now();
uint8_t readMemory(uint8_t offset, uint8_t* data, uint8_t length);
uint8_t writeMemory(uint8_t offset, uint8_t* data, uint8_t length);
};
///In Code
RTC_Base RTC = RTC_DS1307();
DateTime dt = RTC.now();
//The above call just returns a blank DateTime();
代码:
RTC_Base RTC = RTC_DS1307();
DateTime dt = RTC.now(); //The above call just returns a blank DateTime();
这就是对象切片(正如@chris最初猜测的那样)。要使多态性起作用,您必须假装派生类是基类,将指针或引用视为基类,而实际上它是派生类的地址。(因为派生类实际上包含了基类)。
Derived myDerived;
Base &myBaseRef = myDerived;
myBaseRef.myVirtualFunction();
否则,您正在创建派生类,并试图将字节强制转换为Base类,并丢失派生类的所有字节。这可不好!=)
关键是,您实际上不应该将派生类转换为基类,而应该像访问基类一样访问派生类。如果将其转换为基数,则就是基数。并且你的基类返回一个空的DateTime。 对于动态分配的内存,您可以这样做:Base *myBase = nullptr; //Or 'NULL' if you aren't using C++11
myBase = new Derived;
myBase->myVirtualFunction(); //Dereference the myBase pointer and call the function.
delete myBase; //Free the memory when you are finished.
如果你使用的是c++ 11,你可以让std::unique_ptr为你处理对象的生命周期,这样你就不必记得调用'delete':
std::unique_ptr<Base> myBase;
//Later...
myBase = new Derived;
myBase->myVirtualFunction();
//Automatically freed when the myBase smart pointer goes out of scope...
相关文章:
- 在模板基类中为继承类中的可选重写生成虚拟方法
- 方法重写线程C++中的概念
- 如何强制从重写方法调用重写的方法基方法?
- 如何重写全局方法名称以在调用原始方法之前将我的代码推到前面
- C++有没有办法强制重写一组方法,如果其中一个方法在子类中具有重写?
- 在 C++ 中使用模板方法重写类方法
- 检查子类是否执行了方法重写
- C++方法重写和重载(编译器级别)
- 用模板方法重写虚拟方法
- 方法重写(没有虚拟方法或指针)是否被认为是多态性的一部分
- C++ - 方法重写未按预期表示
- 使用两种方法重写<<运算符
- C++阻止方法重写
- C++方法重写
- c++在具体类中强制方法重写
- 方法重载和方法重写的性能问题
- C++中的方法重写是否总是在派生类的标头中完成?
- 是否有更好的方法重写此代码片段
- 为什么静态方法重写基类非静态方法
- 方法重写