继承在Arduino代码 [英] Inheritance in Arduino Code

查看:130
本文介绍了继承在Arduino代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一些Arduino代码,并试图在一些类中使用继承。我有一个类Actor(我的基类)和类大理石(它继承自Actor)。以下是头文件:



Actor.h:

  ifndef Actor_h 
#define Actor_h

#includeArduino.h

类别演员
{
public:
演员();
void speak();
private:
};
#endif

Marble.h:


$ b b

  #ifndef Marble_h 
#define Marble_h

#includeArduino.h
#includeActor.h

class Marble:public Actor {
public:
Marble();
virtual void speak();
private:
};
#endif

Actor.cpp:


$ b b

  #includeArduino.h
#includeActor.h

Actor :: Actor
}

void Actor :: speak(){
Serial.println(Actor);
}

Marble.cpp:

  #includeArduino.h
#includeMarble.h

void Marble :: speak(){
Serial.println(Marble);
}

最后,在循环函数中:

  void loop(){
大理石大理石;
演员儿童[2];
children [0] = marble;

children [0] .speak();

这将导致打印Actor。



我发现这个很好的链接,看起来类似我的问题,但解决方案似乎并不适用于我:
http://arduino.cc/forum/index.php?topic=41884.0



所以。它似乎是当我创建我的数组的演员,并尝试并坚持大理石在那里它被转换为演员,或类似的东西。问题是,我将有一些不同的字符,它们都将继承自Actor,我想要一个数组来遍历它们并调用覆盖的方法。



所以,也许问题是我如何处理这个问题,或者也有一些语法错误?我不知道!



感谢您的帮助,
Kevin

解决方案

您需要在 Actor

中声明 virtual code>类,而不仅仅是在 Marble 类;没有, Actor :: speak 是一个 - 虚拟函数,因此你总是优先于虚拟 Marble :: speak



对于有价值的东西,这与Arduino无关:它只是一个直接的C ++问题。 / p>

I'm writing some Arduino code and attempting to use inheritance in some classes. I have a class "Actor" (my base class) and a class "Marble" (which inherits from Actor). Here are the header files:

Actor.h:

#ifndef Actor_h
#define Actor_h

#include "Arduino.h"

class Actor
{
  public:
    Actor();
    void speak();
  private:
};
#endif

Marble.h:

#ifndef Marble_h
#define Marble_h

#include "Arduino.h"
#include "Actor.h"

class Marble : public Actor {
  public:
    Marble();
    virtual void speak();    
  private:
};
#endif

Actor.cpp:

#include "Arduino.h"
#include "Actor.h"

Actor::Actor()
{
}

void Actor::speak() {
  Serial.println("Actor"); 
}

Marble.cpp:

#include "Arduino.h"
#include "Marble.h"

void Marble::speak() {
  Serial.println("Marble"); 
}

And finally, in the loop function I do:

void loop() {
  Marble marble;
  Actor children[2];
  children[0] = marble;

  children[0].speak();

Which results in "Actor" being printed.

I discovered this nice link which seems similar to my issue, but the resolution does not seem to work for me: http://arduino.cc/forum/index.php?topic=41884.0

So. It seems like when I create my array of "Actors" and try and stick Marble in there it gets cast to an Actor, or something like that. Problem is, I'll have a few different characters that will all inherit from "Actor" and I'd like an array of them to iterate over and call overridden methods on them.

So, perhaps the problem is how I'm approaching this problem, or maybe there's some syntax errors? I don't know!

Thanks for your help, Kevin

解决方案

You need to declare speak as virtual in the Actor class, not just in the Marble class; without that, Actor::speak is a non-virtual function, so you will always be called in preference to the virtual Marble::speak.

For what it's worth, this has nothing to do with the Arduino: it's just a straight C++ issue.

这篇关于继承在Arduino代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆