0

I have a simple set up:

struct Animal {

protected:
    Cry* cry;

};

I can easily access cry in the Constructor of a subclass:

template <typename T>
struct Dog: Animal{

    Dog<T>(Cry* ccry) {
        cry = ccry;
        attacks = Animals::loadAttacks<T>("dog");
    };

private:
    std::vector<T> attacks;
};

When I try to template the Animal like this:

template <typename T>
struct Animal {

protected:
    Cry* cry;
    std::vector<T> attacks;
};

I am unable to access the members like this:

template <typename T>
struct Dog: Animal<T> {

    Dog<T>(Cry* ccry) {
        attacks = Animals::loadAttacks<T>("dog");
        cry = ccry;
    };

};

The compiler complains about attack and cry not being members. I found the compiler to work with the following code:

template <typename T>
struct Dog: Animal<T> {

    Dog<T>(Cry* ccry) {
        attacks(Animals::loadAttacks<T>("dog"));
        this->cry= ccry;
    };

};

but it throws a "Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0" which leads me to believe that the members are not being initialized correctly.

How does one go about this?

John Glen
  • 771
  • 7
  • 24
  • Just do with `attacks` the same thing you did with `cry`: `this->attacks = Animals::loadAttacks("dog");`. Your code as written likely finds a function named `attacks` somewhere in the enclosing scope, and calls that. – Igor Tandetnik Jul 26 '20 at 17:22
  • It's a bit odd (to me, at least) for a derived class constructor's body to be (re)initializing the member variables of the base class. Why not pass them in as arguments to the base class constructor in the derived class initializer list? – Eljay Jul 26 '20 at 17:29

0 Answers0