954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

pure virtual function in constructor

after i've got this error:
pure virtual method called
terminate called without an active exception
Abort (core dumped)

i searched and found that it is probably because i'm calling to a pure virtual function from a constructor.
well, yes, i'm doing that, but -
- what is wrong with that? (what the idea that is behind it?)
- how can i avoid it? i have a father class that have a data member that should be calculated, but - the calculation is different for every son. so i wanted to call the pure virtual function to calculate it, and the received int to save in the data member (that is private in the father).


i hope i wasn't too not-understandable.. :)

anilopo
Light Poster
29 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

what is wrong with that? - Well, a pure virtual function has no implementation itself, it forces the descendant classes to actually implement it so calling it would obviously toss an error.

how can I avoid it? - Make sure whatever class you're passing to whatever method that is calling the virtual method actually implements that function IE., if the method is supposed to take a Vehicle which has a pure virtual method move() make sure you're only passing it a descendant class like Car or Boat (in other words, classes that actually implement Vehicle 's method move()

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Post your code, please.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
Post your code, please.


it is pretty long, but the idea:

class Product{
   ...
   private:
      int price;
      virtual int calc () = 0;

   public:
      Product (...);
};

Product :: Product (...)
{
   price = calc ();
}

in all the grandsons the calc function is implemented.


ShawnCplus:
i'm not sure i understand what u r saying, but this pure virtual functionhave an implementation in the grandsons (although not sons - it supposed to b ok).

anilopo
Light Poster
29 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

You can't call calc() in the product class. calc() isn't implemented in the Product class! ;)

What you're doing in the ctor is basically:

Product :: Product (...)
{
   this->price = this->calc ();
}


And since "this" in that context is a Product*, you can't call calc().

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

Obviously you can't call calc from the Product class constructor because it's not implemented. You can call it from the child class constructors where calc is implemented, but it's potentially dangerous. You might be relying on parts of the object that haven't been constructed. That's why I wanted to see your code, to determine if you were doing something dangerous where calling calc in the child constructors would be unwise.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

You can't call calc() in the product class. calc() isn't implemented in the Product class! ;)

What you're doing in the ctor is basically:

Product :: Product (...)
{
   this->price = this->calc ();
}

And since "this" in that context is a Product*, you can't call calc().


hmmm...
from what i understand - this is a problem just if i'm trying to call "calc" from the constructor, not from every function of Product.
(i've also checked something resembling that, and there was no problem)

anilopo
Light Poster
29 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 
Obviously you can't call calc from the Product class constructor because it's not implemented. You can call it from the child class constructors where calc is implemented, but it's potentially dangerous. You might be relying on parts of the object that haven't been constructed. That's why I wanted to see your code, to determine if you were doing something dangerous where calling calc in the child constructors would be unwise.


so where can i call the calc function?
the idea is that i can't assign in the private data member in the sons.

anilopo
Light Poster
29 posts since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

>from what i understand - this is a problem just if i'm trying to call
>"calc" from the constructor, not from every function of Product.

Constructors are not inherited. Because Product is an abstract class, the only possible direct use is polymorphic. You're using a pointer or reference to Product, but the actual type pointed to is one of the children. By that time you're accessing the child, not the parent, and calc is defined.

>so where can i call the calc function?
As I said, you can call pure virtual functions in the child constructor, provided that child implements the function and provided that the implementation is safe for a partially constructed child object.

>the idea is that i can't assign in the private data member in the sons.
You can't assign to private data, but you can call public/protected member functions and the base class constructor. You don't need virtuals to do this:

class Product {
    int _price;
public:
    Product(int price): _price(price) {}
    int price() { return _price; }
};

class MyProduct: public Product {
    int _quantity;
public:
    MyProduct(int quantity)
        : Product(calc()), _quantity(quantity)
    {}
    int quantity() { return _quantity; }
private:
    int calc() { return 12345; }
};

#include <iostream>

int main()
{
    Product *p = new MyProduct(10);

    std::cout<< p->price() <<'\n'<< ((MyProduct*)p)->quantity() <<'\n';
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Hey, sorry to go offtopic anilopo, but Narue reminded me,

If I have a class like this:

class X{
public:
  X(int y) : y(y) {}
 
private:
  int y;
}


Does the ctor invoke undefined behavior or is it valid C++? And does it do what I expect it to do: Set X::y to ctor param y? It works in MinGW GCC 3.4 afaik.

Again sorry for offtopic.

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 

>Does the ctor invoke undefined behavior or is it valid C++?
It's valid and required to initialize the data member y with the value of the parameter y. In the constructor body it's a different matter entirely, which is why you have to prefix the data member with this :

class X{
public:
  X(int y)
  {
    this->y = y;
  }
private:
  int y;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

>Does the ctor invoke undefined behavior or is it valid C++? It's valid and required to initialize the data member y with the value of the parameter y. In the constructor body it's a different matter entirely, which is why you have to prefix the data member with this :

class X{
public:
  X(int y)
  {
    this->y = y;
  }
private:
  int y;
}


To add to Narue's post, they're called initialization lists if you want to research them on your own.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You