0

Thanks for taking a look at my post, hopefully it will be relatively simple.

I am attempting to learn more about QP/C or QP/nano, but as part of understanding how it works I need to learn about OOP concepts in C. Which is detailed in:

https://www.state-machine.com/doc/AN_OOP_in_C.pdf

I have worked through the encapsualation portion of the OOP document and now am working to understand inheritance. In the document they show two ways of upcasting from the inherited class to a super class:

Shape_moveBy((Shape *)&r1, -2, 3);

and then

Shape_moveBy(&r2->super, 2, -1);

But I can't get this code to work?

To understand this concept I tried to make some example code:

/****************** Inheritance Example ***********************/

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

/* Shape Class */
typedef struct
{
    int16_t x; /* x-coordinate of shape's position */
    int16_t y; /* y-coordinate of shape's position */
} Shape;

void Shape_ctor(Shape * const me, int16_t x, int16_t y);
void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy);

void Shape_ctor(Shape * const me, int16_t, int16_t y)
{
    me->x = x;
    me->y = y;
}

void Shape_moveBy(Shape * const me, int16_t dx, int16_t dy)
{
    me->x += dx;
    me->y += dy;
}
/* End Shape Class */

/* Rectangle Class (Inherited Class below Shape) */
typedef struct
{
    Shape super; // inherits shape attributes
    uint16_t width;
    uint16_t height;
} Rectangle;

void Rectangle_ctor(Rectangle * const me, int16_t x, intt16_t y, 
                    uint16_t width, uint16_t height);

Void Rectangle_ctor(Rectangle * const me, int16_t x, intt16_t y, 
                    uint16_t width, uint16_t height)
{
    Shape_ctor(&me->super, x, y);
    me->width = width;
    me->height = height;
}
/* End Rectangle Class */

int main()
{
Rectangle r1, r2;

// construct rectangles
Rectangle_ctor(&r1, 0, 2, 10, 15);
Rectangle_ctor(&r2, -1, 3, 5, 8);

// operate on the shape
Shape_moveBy((Shape *)&r1, 2, 3);
Shape_moveBy(&r2->super, 2,-1); 

return 0;
}

when I compile I get the error

oop_inher.c In function 'main'
oop_inher.c:77:17: error: invalid type argument '->' (have 'Rectangle {aska struct <anonymous>}')
 Shape_moveBy(&r2->super, 2,-1);

I am guessing I am not using pointers correctly here, but since I am attempting upcasting I have a hard time following what I am doing wrong.

DisplayName001
  • 253
  • 2
  • 3
  • 8
  • The problem is that you're using the "arrow" operator `->` to access a structure member, but the structure you have is not a pointer. Perhaps you should review structure member access in [one of your beginners books](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list)? – Some programmer dude Jan 04 '18 at 12:32
  • Thanks Some Programmer dude your comment was both insightful and demeaning – DisplayName001 Jan 04 '18 at 12:39

1 Answers1

0

What if you replace

Shape_moveBy(&r2->super, 2,-1);

By

Shape_moveBy(&r2.super, 2,-1);

Alan Hortz
  • 85
  • 10