-3
class Base1{
    public:
        Base1(){};
        virtual ~Base1() = 0;
}

class Derived1 : public Base1{
    public:
        Derived1(int a) : a(a){};
        ~Derived1();
        int a;
}

class Base2{
    public:
        Base2(){};
        virtual ~Base2() = 0;
}

class Derived2 : public Base2{
    public:
        Derived2(int b) : b(b){};
        ~Derived2();
        int b;
        void func(const Base1 &base1); // How to access Derived1::a here?
}

Given the above class definition, how can I access Derived1::a in void func(const Base1 &base1)? I am still new to polymorphism. I tried to use different static_cast or dynamic_cast methods but none of them works. What should I do inside the function so I can access a derived class member from a base class reference?

FYI I can't change the class definition for my coursework requirement, and that is what given to me. I understand that it is simpler to just pass Derived1 as parameter but I am not allow to do so.

kyktyj
  • 3
  • 2
  • `Given the above class definition, how can I access Derived1::a in void func(const Base1 &base1)?` You don't. Your parameter should be a `Derived1`, not a `Base1`. – tkausl Mar 23 '22 at 16:08
  • 4
    If you require `base1` to reference a `Derived1`, why is the parameter not `const Derived1&`? – user17732522 Mar 23 '22 at 16:08
  • You have a misunderstanding my guy. You cannot access members of a derived class from the base class. So just use `Derived1` instead of `Base1`. – The Coding Fox Mar 23 '22 at 16:08
  • 1
    You may access any virtual class member functions declared `const`, you also can use a `dynamic_cast(base1)`, but I'd not really recommend that. – πάντα ῥεῖ Mar 23 '22 at 16:11
  • I am asked to follow such a function definition in my coursework's skeleton codes so I don't get what should I do. @tkausl – kyktyj Mar 23 '22 at 16:13
  • @kyktyj - If that's what your coursework requires you to use, you should followup with your instructor. – Stephen Newell Mar 23 '22 at 16:25
  • @StephenNewell Ok, thanks for letting me know that it isn't a good programming practice. – kyktyj Mar 23 '22 at 16:28
  • Do what you have to do to pass the class. The instructor may get back to this later and explain why it's a bad idea or it may be covered in your course readings. If it isn't, you may want to upgrade your understanding of the language and idioms with some self-study and [a few good books](https://stackoverflow.com/questions/388242) to ease your entry into the workforce. – user4581301 Mar 23 '22 at 16:34

1 Answers1

2

Given the above class definition, how can I access Derived1::a in void func(const Base1 &base1)? ... FYI I can't change the class definition for my coursework requirement, and that is what given to me.

Ideally, you should expose a virtual method in Base1 that returns an int (or int&), and then have Derived1 override that method to return its a member.

But, given that you are not allowed to change the class definitions, that is not an option.

You need a pointer or reference to a Derived1 object in order to access its a member directly. Which really leaves you with only 1 choice - you can use dynamic_cast to typecast the base class reference to the derived class type, eg:

void Derived2::func(const Base1 &base1)
{
    // this will raise an exception if the cast fails at runtime
    const Derived1 &d = dynamic_cast<const Derived1&>(base1);
    // use d.a as needed...
}

Alternatively:

void Derived2::func(const Base1 &base1)
{
    // this will return null if the cast fails at runtime
    const Derived1 *d = dynamic_cast<const Derived1*>(&base1);
    if (d) {
        // use d->a as needed...
    } else {
        // do something else...
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770