Inheritance usage resulting in code restart.
Posted: Mon Nov 18, 2019 11:13 am
When I am using inheritance and trying to call the child class function using base class pointer code is restarting. When using the child class pointer then also code is restarting. But when using it with out inheritance its working fine. Kindly help me to resolve this.
Code using base class pointer, code is restart when trying to call getabc().
class base{
public:
virtual int getabc() = 0;
};
class test: public base{
public:
int getabc(){
Serial.println("okk");
return 1;
}
};
base* ptr;
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
ptr = (test*)malloc(sizeof(test));
while(!Serial);
ptr->getabc();
}
void loop() {
ptr->getabc();
Serial.println("okk");
delay(5000);
}
When using the child class pointer then also code is restarting.
Following code is also restarting :
class base{
public:
virtual int getabc() = 0;
};
class test: public base{
public:
int getabc(){
Serial.println("okk");
return 1;
}
};
test* ptr;
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
ptr = (test*)malloc(sizeof(test));
while(!Serial);
ptr->getabc();
}
void loop() {
ptr->getabc();
Serial.println("okk");
delay(5000);
}
But when using it with out inheritance its working fine. Kindly help me to resolve this.
Code that is working:
class base{
public:
virtual int getabc() = 0;
};
class test{
public:
int getabc(){
Serial.println("okk");
return 1;
}
};
test* ptr;
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
ptr = (test*)malloc(sizeof(test));
while(!Serial);
ptr->getabc();
}
void loop() {
ptr->getabc();
Serial.println("okk");
delay(5000);
}
Code using base class pointer, code is restart when trying to call getabc().
class base{
public:
virtual int getabc() = 0;
};
class test: public base{
public:
int getabc(){
Serial.println("okk");
return 1;
}
};
base* ptr;
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
ptr = (test*)malloc(sizeof(test));
while(!Serial);
ptr->getabc();
}
void loop() {
ptr->getabc();
Serial.println("okk");
delay(5000);
}
When using the child class pointer then also code is restarting.
Following code is also restarting :
class base{
public:
virtual int getabc() = 0;
};
class test: public base{
public:
int getabc(){
Serial.println("okk");
return 1;
}
};
test* ptr;
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
ptr = (test*)malloc(sizeof(test));
while(!Serial);
ptr->getabc();
}
void loop() {
ptr->getabc();
Serial.println("okk");
delay(5000);
}
But when using it with out inheritance its working fine. Kindly help me to resolve this.
Code that is working:
class base{
public:
virtual int getabc() = 0;
};
class test{
public:
int getabc(){
Serial.println("okk");
return 1;
}
};
test* ptr;
void setup() {
Serial.begin(115200);
// put your setup code here, to run once:
ptr = (test*)malloc(sizeof(test));
while(!Serial);
ptr->getabc();
}
void loop() {
ptr->getabc();
Serial.println("okk");
delay(5000);
}