1

I want to create an abstract class that has a pure virtual function that is called by the constructor that is NOT purely virtual. Below is my file class.hpp:

#ifndef __CLASS_HPP__
#define __CLASS_HPP__

#include <iostream>

class Parent {
 public:
  Parent(){
    helloWorld(); // forced to say hello when constructor called                    
  };
  virtual void helloWorld() = 0; // no standard hello...                            
};

class Child : public Parent {
 public:
  void helloWorld(){ // childs implementation of helloWorld                         
    std::cout << "Hello, World!\n";
  };
};

#endif

In this example, I have a parent class that has a pure virtual function helloWorld(). I want every derived class to say "hello" when the constructor is called; hence why helloWorld() is in the parent class constructor. However, I want every derived class to be FORCED to choose how its going go say "hello", rather than having a default method. Is this possible? If I try to compile this with g++, I get the error that a pure virtual function is being called by the constructor. My main.cpp is:

#include "class.hpp"

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

I am compiling using g++ main.cpp -o main.out and the resulting error is:

In file included from main.cpp:1:0:
class.hpp: In constructor ‘Parent::Parent()’:  
class.hpp:9:16: warning: pure virtual ‘virtual void Parent::helloWorld()’ called from constructor [enabled by default]

Any suggestions for how to get a similar setup in a legal way?

NEW QUESTION

DyP has brought to my attention that a constructor does not use any overridden functions, so what I want to be able to do isn't possible in the way I have it setup. However, I still would like to force any derived constructor to call the function helloWorld(), is there any way to do this?

8
  • Calling a virtual function from a ctor (of the same object) is dangerous, because it doesn't call overrides from derived classes. In this case, it tries to call Parent::helloWorld, which is pure, hence the warning (a pure virtual function can still have an implementation). You could pass a string from the derived class to the base class ctor via a parameter, and do the output in the base class. Also possible: 2-stage init.
    – dyp
    Sep 11, 2013 at 3:32
  • It seems strange for a base class to depend on a derived class like this. Can you elaborate on the problem you're trying to solve? Depending on the details, there may be other options available.
    – Ben S.
    Sep 11, 2013 at 4:04
  • In my actual problem, I have a class that holds data for a simulation. However, in the simulation there are a lot of features that are dependent on the user's preferences. I have the base class be an abstract class with a constructor that reads in a lot of data and stores it, but leaves a few functions abstract (these are the ones depending on uesr preference). So I want any user to be forced to read the data in the same way but also be forced to implement those abstract functions.
    – nick
    Sep 11, 2013 at 12:41
  • Names that contain two consecutive underscores (__CLASS_HPP__) and names that begin with an underscore followed by a capital letter are reserved to the implementation. Don't use them. Sep 11, 2013 at 12:56
  • @PeteBecker what do you mean?
    – nick
    Sep 11, 2013 at 14:29

2 Answers 2

2

What you are doing is illegal.

In order to define a abstract class in C++ your class must have at least one pure virtual function. In your case

virtual void helloWorld() = 0;

in this case you are right.

But your pure virtual function have no any implementation, since it is a pure virtual function. So it is illegal to call pure virtual function from the constuructor of the same class.(In class level pure virtual function have no any implementation)

So,

Parent(){
helloWorld(); // forced to say hello when constructor called                    
};

this is illegal.

If you want, you can implement your pure virtual function in derived class and then call helloWorld() from constructor of derived class

2
  • That would not be what I wanted. I want every child class to be forced to call helloWorld(). The code I am writing will be used by others and I want them to be forced to call that function.
    – nick
    Sep 11, 2013 at 12:38
  • I got it. That is why, you call 'helloWorld()' inside parent class constructor. But the case is,if you want to define a abstract class it must contain at least one pure virtual function. In your case it is 'helloWorld()' and it have no implementation in parent class level.
    – ANjaNA
    Sep 12, 2013 at 3:19
1

Why don't you simply add it to the constructor of the every child class?

If you want to avoid writing it every time in the constructor (or even skipping or inheriting it) then you could use CRTP:

class Parent {
 public:
  Parent(){};
  virtual void helloWorld() = 0; // no standard hello...                            
};

template <typename Par>
class ParentCRTP: public Parent {
 public:
  ParentCRTP(){
    Par::doHelloWorld();
  };
  virtual void helloWorld(){
    Par::doHelloWorld();
  }
};

class Child : public ParentCRTP<Child> {
 public:
  static void doHelloWorld(){ // childs implementation of helloWorld                         
    std::cout << "Hello, World!\n";
  };
};

This approach will not give you a pointer to the child class in your child's hello method - at this point class instance is only Parent instance, no valid Child pointer can be obtained. To force an execution of Child's method after construction you can only use two stage initialization: First you create class instance using a constructor, and then initialize it using separate method.

Apart from that, problem like this is probably a hint to re-think your design. You shouldn't force your classes to initialize itself in a given way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.