Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Advice: pure virtual method in base class implemented in subclass
Forum Updated to NodeBB v4.3 + New Features

Advice: pure virtual method in base class implemented in subclass

Scheduled Pinned Locked Moved C++ Gurus
5 Posts 3 Posters 5.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fluca1978
    wrote on last edited by
    #1

    Hi all,
    what I'd like to achieve is something like this:

    @class A{
    protected:
    virtual myType* createMyType() = 0;
    void doSomeWork(){ createMyType(); }
    };

    class B : public A {
    myType* createMyType() { ... }
    };

    @

    (I mixed the declaration and the definition for brevity).
    So in the base class I'd like to have a pure virtual method that is used by another (not virtual) method in the base class itself. In the subclass I'd like to implement the virtual method so to change the behavior, but this is not working since the base class gives me a "pure virtual method call" in the doSomeWork method. Is there a way to achieve this (or a better pattern to do this)?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      AFAIK, this is possible, as long as the call is not made from the constructor. During construction, the type is not yet complete, and virtual function calls fail miserably. However, after construction, it should work just fine.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        fluca1978
        wrote on last edited by
        #3

        Ah I see... in fact the call is in my constructor path. I think I have to refactor the code, since having a polymorphic method within the constructor is quite dangerous.
        Thanks

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on last edited by
          #4

          Calling virtuals in constructors: Not only it's dangerous, it plain doesn't work.

          This "C++ FAQ":http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.6 shows some recipes how to circumvent that problem.

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • F Offline
            F Offline
            fluca1978
            wrote on last edited by
            #5

            [quote author="Volker" date="1324298291"]Calling virtuals in constructors: Not only it's dangerous, it plain doesn't work.

            [/quote]

            Thanks, I know it is bad. In fact I'm going to change it. I simply did not realize from the compiling error that it was in my constructor path.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved