Before logging an issue, please update to the latest release of Visual Micro from the Downloads Page.

When Logging a Support Issue in the Forum, please ensure you have also:-

  • Enabled vMicro > Compiler > Show Build Properties
  • Re-Compile your program with these settings enabled
 
Save the new Output to a Text File and....
  • Click the Reply button and attach as .txt file OR
  • Click here to Email us with the file attached, and a link to your post
Support requests without the output above may be impossible to answer, so please help us to help you
 
Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Abstract Class Problem (Read 2484 times)
Bob Jones
Full Member
***
Offline


Posts: 210
Location: Bellingham, WA
Joined: Dec 4th, 2015
Abstract Class Problem
Jun 16th, 2016 at 7:02am
Print Post  
Given this code in "Classes.h"

class TopClass {
public:
     int topValue = 1;
     int getTop();
     virtual int getMiddle() = 0;
     virtual int getBottom() = 0;
};

class MiddleClass : public TopClass
{
public:
     int middleValue = 2;
     virtual int getMiddle();
};

class BottomClass : public MiddleClass
{
public:
     int bottomValue = 3;
     int getBottom();

};

int TopClass::getTop() {
     return topValue;
}
int MiddleClass::getMiddle() {
     return middleValue;
}
int BottomClass::getBottom() {
     return bottomValue;
}


#include "Classes.h"

TopClass *top;

void setup() {
     Serial.print("top:"); 
        Serial.println(top->topValue);
     Serial.print("middle:"); 
        Serial.print(top->getMiddle());
     Serial.print("bottom:"); 
        Serial.print(top->getBottom());
}

The output is "top:top:top:top:..." and the program restarts over and over. 

What I am trying to do is understand how to declare an abstract base class and access methods and properties in it's subclasses. This code compiles just fine (and no warnings), but it doesn't work at all.
  
Back to top
 
IP Logged
 
Tim@Visual Micro
Administrator
*****
Offline


Posts: 12191
Location: United Kingdom
Joined: Apr 10th, 2010
Re: Abstract Class Problem
Reply #1 - Jun 16th, 2016 at 6:50pm
Print Post  
Okay a couple of things that might help.

The debugger works in .ino and .cpp but not currently in .h files.

Normally it is expect that a .h simply includes the prototypes and the .cpp the code.



Other user might find this useful. I would avoid using the far pointers. If you want your classes to be static (onloy ever one instance then please say because there are other ways to do that.

Right click the project and select "Add>New Arduino Class and Header". At the prompt that appears enter "MyButtonClass" and click OK

Look at the .cpp and .h that have been created. You will find that in your loop() you can use this code:-

Code
Select All
void setup()
{
    MyButton.init();
} 



Note that at the bottom of each file the MyButton object is created. The header shows it as an "extern" prototype you won't need these entries if you create the object in the same code file where it is to be used.

For example we could create a second instance of the class called MyButton2 in the .ino code like this:-

Code
Select All
MyButtonClass MyButton2;

void setup()
{
	MyButton2.init();
} 




If I declare and add one to variable "i" in the MyButtonClass::init() method of the .cpp and add a debug breakpoint the debugger stops on the line of code after MyButton.init() has been hit.

Code
Select All
#include "MyButtonClass.h"
int i;
void MyButtonClass::init()
{

	i++;

} 

« Last Edit: Jun 16th, 2016 at 6:54pm by Tim@Visual Micro »  
Back to top
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint