mardi 26 juin 2018

Derived classes from abstract base class

I am trying to use a derived class from an abstract base class in c++ (c++11). I can not post exact code but here is an example of my implementation:

class A{
    protected:
        union x {
          struct type_a{
              uint8_t data : 8;
          };
          struct type_b{
              uint8_t data : 12;
          }; 
        };
    public:
        virtual int get_word() = 0; //pure virtual, makes class abstract
};

class B : public A{
    private:
        struct type_var{}type; //used to store struct type from base class
    public:
        B(int select) {
            switch(select) {
                case CNTR_GRP:
                   type = x.type_a; //error here
                break;
                /*... more similar case statements */
        }
        //implement get_word().. etc
};

Now like I said, I can only show example code. I need the derived class to select the correct struct type from the base (abstract) class. The type of the struct is not known until runtime so this is why it is structured like so. My current error is error: expected primary-expression before ‘.’ token which occurs where I commented. I have tried many different ways of accessing and setting the proper struct from base class such as type = A::x.type_a; but have failed to get it to work.

I have researched and read lots of posts on SO and tried following implementations and examples but can't get it to work. Am I missing something here? Thanks for the help, hopefully I'm not missing something completely obvious.

Resources I have used:

Interfaces in C++ (Abstract Classes)

How to access protected members in derived class?

Why does a purely virtual/abstract class require a constructor, in particular for protected const member variables?

  • several more that I closed and can't seem to find.

Aucun commentaire:

Enregistrer un commentaire