0

I was wondering why can't i create something like this ? :

class Abstract_object
{
protected:
    virtual void _initialize() = 0;

public:
    Abstract_object()
    {
        _initialize();
    }
};

class Object : public Abstract_object
{
protected:
    void _initialize()
    {
         std::cout << "Initate client !" << std::endl;
    }

public:
    Object () : Abstract_object()
    {
        
    }
};

int main()
{
    Object* tmp = new Object();

    return (0);
}

When i try to compile it, i recieved a unresolved external symbol, which i quite don't understand where it came from : the definition of _initialize is indeed found in the children ... Someone can explain to me what is happening ? =)

i have tried to call _initiate inside the children class, and it did work as intended. But i will have to create multiple class enhancing Abstract class, and i do not want to call _initiate inside each one of them, so i'm looking for a turnaround, if someone have it =)

Thank you guys ! =)

This is the error i got when compiling this : Error LNK2019 unresolved external symbol "protected: virtual void __thiscall Abstract_object::_initialize(void)" (?_initialize@Abstract_object@@MAEXXZ) referenced in function "public: __thiscall Abstract_object::Abstract_object(void)" (??0Abstract_object@@QAE@XZ)

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Hyarius
  • 1
  • 1

1 Answers1

0

In the constructor there is used the function _initialize

Abstract_object()
{
    _initialize();
}

Though it is a pure virtual function nevertheless you need to define it for the class Abstract_object. For example write after the class definition

void Abstract_object::_initialize()
{
    //...some code
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Yes, thats' exactly the question : is it possible to call a virtual function inside an abstract constructor ? I have defined it inside the children definition – Hyarius Mar 05 '22 at 21:19
  • @Hyarius It is possible to call it. The virtual function of the base class will be called. – Vlad from Moscow Mar 05 '22 at 21:19
  • Yes, so it's impossible to call it in the constructor AND having it pure abstract. I can have it virtual but not abstract – Hyarius Mar 05 '22 at 21:21
  • 1
    @Hyarius There is no such a notion as the abstract function. There are abstract class and virtual function. – Vlad from Moscow Mar 05 '22 at 21:22
  • what "abstract method" don't exist ? – Hyarius Mar 05 '22 at 21:22
  • @Hyarius In C++ there is no such a notion. – Vlad from Moscow Mar 05 '22 at 21:23
  • In C++, an abstract method is just another way of describing the characteristics of a pure virtual function. Both just mean a method with no implementation provided that needs to be implemented in a sub-class before the class can actually be instantiated. – Hyarius Mar 05 '22 at 21:24
  • @Hyarius Even if you could get the code to compile, it simply can't work the way you want. You can't call an overridden child method from a base class constructor. – Remy Lebeau Mar 05 '22 at 21:25
  • @Hyarius As I have written in C++ there is no such a term. – Vlad from Moscow Mar 05 '22 at 21:26
  • Thank, that's the awnser i wanted =) You simply can't call a abstract method inside the constructor of said abstract class. Wasted 1h on this problem, i don't need to search more ^^ Thanks =) – Hyarius Mar 05 '22 at 21:26