Linker error when calling pure virtual function with gcc

My friend and I had a very interesting discussion about the construction of objects, which ended with this piece of code:

#include <iostream>

class Parent {
  public:
    Parent( ) {
      this->doSomething( );
    }

    virtual void doSomething( ) = 0;
};

class Child : public Parent {
    int param;

  public:
    Child( ) {
      param = 1000;
    }

    virtual void doSomething( ) {
      std::cout << "doSomething( " << param << " )" << std::endl;
    }
};

int main( void ) {
    Child c;
    return 0;
}

I know that the standard does not define the behavior when a pure virtual function is called from a constructor or destructor, it is also not a practical example of how I will write code in the production process, this is just a test to check what the compiler does.

Testing the Same Design in Java Printing

doSomething (0)

This makes sense because it is paramnot initialized at the point doSomething()called from the parent constructor.

I would expect similar behavior in C ++, with the difference that it paramcontains something at the time the function is called.

(c++ (Ubuntu 4.4.3-4ubuntu5.1) 4.4.3), , Parent::doSomething( ) undefined.

, : ? , , , , . , , , .

! , , .

+5
3

, : ? , , , , . , , , .

.

?

Parent::doSomething() , .

, , , .

. , , Parent::doSomething() . , , -, . , , , Child::doSomething(), Parent::doSomething(), Parent::doSomething() ,

?

, ( , ), :

struct base {
   virtual void f() = 0;
};
inline void base::f() { std::cout << "base\n"; }
struct derived : base {
   virtual void f() {
      base::f();
      std::cout << "derived\n";
   }
};
int main() {
   derived d;
   d.f();        // outputs: base derived
}

++ , , . Parent::doSomething() , . , - TU , , , , , .

, , , .

, ( ) Parent. , , , , , .

- , ( odr ) . - . , , , (Parent::doSomething) (vtable), , .

+5

, doSomething , doSomething , . , . , .

+5

, doSomething(), , * Parent, . /.

0

All Articles