【发布时间】:2014-04-26 18:15:31
【问题描述】:
我现在在互联网和论坛上搜索了 3 个小时,但我找不到适合我的正确答案。
过去我只做 C ... 但现在我想用 C++ 做一些 Arduino 的东西(编写一个新的 Lib)。
这里有一个(简化的)我的问题示例:
Class Base
{
public:
uint8_t buffer[ 20 ];
uint8_t bufferLength;
protected:
boolean receiveByte( uint8_t * buf, unint8_t * bufLen );
}
boolean MyClass::receiveByte( uint8_t * buf, unint8_t * bufLen )
{
if( Serial.available() > 0 )
{
buf[ *buflen++ ] = Serial.read();
return( true );
}
return( false );
}
Class MyClass : public Base
{
public:
void execute( void );
}
void MyClass::execute( void )
{
if( receiveByte( buffer, &bufferLength ) == true )
{
// Do something
}
}
为什么当我在新继承的类中通过reveiveByte() 方法使用bufferLength 的地址时,bufferLength 没有递增?
【问题讨论】:
-
我们怎么知道?您没有向我们提供
receiveByte定义。提供了receive方法,这就是您要问的吗?你的意思是那些方法是virtual? -
请注意,这不是使用“长度”参数的正常方式。
-
如果您收到超过 20 个字节,您将度过糟糕的一天。
-
@CaptainObvlious:
Serial.read()只返回一个字节,代码中没有循环。 -
当然......在arduino草图中像往常一样有一个循环!^^但它不在我的简化示例中......对不起! ;p
标签: c++ class pointers inheritance arduino