1

Take a look at this small C++ program:

#include <iostream>

#define debugStream std::cout

struct Id {
    Id() {
        debugStream << this->getClassName() << " created\n";
    }

    virtual ~Id() {
        debugStream << this->getClassName() << " destroyed\n";
    }

    virtual std::string getClassName() {
        return "Id";
    }
};


struct D : public Id {
    std::string getClassName() {
        return "D";
    }
};

int main( int argc, const char *argv[] ) {
    Id* i = new D();
    return 0;
}

I expected this program to print:

D created
D destroyed

but instead it prints:

Id created
Id destroyed

virtual is working as expected for the destructor but not for the getClassName function. Why is it so?

saga
  • 1,933
  • 2
  • 17
  • 44
  • 2
    `vitual` functions don't behave polymorphically when called from the constructor. See the duplicate. – François Andrieux Oct 19 '18 at 15:34
  • @FrançoisAndrieux I think this question should be kept open since it's very difficult to figure out that the problem was because of calling the function in a constructor. – saga Oct 19 '18 at 15:36
  • 3
    Unless you can identify how this question is significantly different from the duplicate, the correct thing is to leave it closed. If anyone finds your question, they will be directed to the duplicate. Having all duplicates point to a single post allows for the all the answers to be easy to find from any duplicate. Closing a question doesn't necessarily mean that the question is bad. – François Andrieux Oct 19 '18 at 15:39
  • Ok, I confused `closed` with `deleted` – saga Oct 19 '18 at 15:39
  • @FrançoisAndrieux Unless it gets roomba'd ;) – Max Langhof Oct 19 '18 at 15:40
  • @MaxLanghof I don't think posts closed because of a duplicate qualifies it for automatic deletion. Edit : https://stackoverflow.com/help/roomba has a clause "If the question was closed more than 9 days ago, and not closed as a duplicate...". Duplicates are useful to keep around. – François Andrieux Oct 19 '18 at 15:41
  • @FrançoisAndrieux Oh ok, that's good to know. – Max Langhof Oct 19 '18 at 15:49
  • @FrançoisAndrieux This code is supposed to log the creation of objects in my code, I was trying to use this code to debug RAII for some containers I implemented. Is it possible to achieve this functionality in c++? Should I open another question for this? – saga Oct 19 '18 at 16:04
  • @saga When you are in the base type constructor, the derived type hasn't begun existing yet. Perhaps you can make the base type have a templated constructor such that the template argument is deduced to be the derived type. Then, you could have a `static` `getClassName` member function for each derived type. But I'm not sure how much you are able to change the existing architecture to accommodate it. – François Andrieux Oct 19 '18 at 16:46
  • @saga I've added a solution to the duplicate that reflects my last comment. The solution's example is largely based on your question. – François Andrieux Oct 19 '18 at 17:01

0 Answers0