Problems with C ++ abstract class (I can do it in Java, but not in C ++!)

First of all, I looked for this problem and found many similar questions, but I could not find the answer that fixed my problem. I'm sorry if it's just me, dumb.

What I'm trying to do is make the abstract class constructor call the function that is virtual. In Java, this works because a subclass provides an implementation of the abstract method that is called. However, in C ++, I get this linker error:

test.o:test.cpp:(.text$_ZN15MyAbstractClassC2Ev[MyAbstractClass::MyAbstractClass
()]+0x16): undefined reference to `MyAbstractClass::initialize()'
collect2: ld returned 1 exit status

Here is my code:

#include <iostream>

class MyAbstractClass {
protected:
    virtual void initialize() = 0;

public:
    MyAbstractClass() {
        initialize();
    }
};

class MyClass : public MyAbstractClass {
private:
    void initialize() {
        std::cout << "yey!" << std::endl;
    }
};

int main() {
    MyClass *my = new MyClass();
    return 0;
}

As an additional explanation of what I'm trying to do, here is the Java code that accomplishes my goal:

public abstract class MyAbstractClass {

    public MyAbstractClass() {
        initialize();
    }

    protected abstract void initialize();
}

public class MyClass extends MyAbstractClass {

    protected void initialize() {
        System.out.println("Yey!");
    }

    public static void main(String[] args) {
        MyClass myClass = new MyClass();
    }
}

This code prints "Yey!". Any help is much appreciated!

+5
source share
6
MyAbstractClass() {
    initialize();
}

MyClass::initialize(), MyClass . , MyAbstractClass::initialize() , , . (, -.)

- , , . .

initialize() ; .


Update

, , , - Undefined . !

+4

++ ( ). , , , , .

; , :

#include <iostream>

class MyAbstractClass {
public:
    MyAbstractClass() {
        // don't do anything special to initialise the derived class
    }
};

class MyClass : public MyAbstractClass {
public:
    MyClass() {
        std::cout << "yey!" << std::endl;
    }
};

int main() {
    MyClass my;
    return 0;
}

, my ; , , RAII , .

+2

(. ):

9: .

: , , , , . Java # -, , , , ++ zags.

: . , , . . , ( ) , initialize , !

+1

++ , Java. - , ++. , , , , .

, , , , . ++ , , , . - [*]. (, ), .

[*] . .

Java , , . Java, , ( ++ : ), . , , initialize():

public class MyClass extends MyAbstractClass {
   final int k1 = 1;
   final int k2;
   MyClass() {
      k2 = 2;
   }
   void initialize() {
      System.out.println( "Constant 1 is " + k1 + " and constant 2 is " + k2 );
   }
}

? ( )

, , , MyClass , . , , . , , , reset :

public class MyClass extends MyAbstractClass {
   Logger logger;
   MyClass() {
      logger = new Logger( System.out );
   }
   void initialize() {
      logger.debug( "Starting initialization" );
   }
}

, , . MyClass, , - . logger , . , MyAbstractClass , , NullPointerException.

, ++, , , , , (, , , ).

(): /JVM, , , Constant 1, 1, 2 0. , , , ... 1/0 , , , . , k1 MyAbstractBase, initialize() , MyBase ).

+1

. , ( , ).

0

@Seth, . , . - initialize , .

-1

All Articles