pure virtual function in constructor

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 16
Reputation: anilopo is an unknown quantity at this point 
Solved Threads: 0
anilopo anilopo is offline Offline
Newbie Poster

pure virtual function in constructor

 
0
  #1
Jun 15th, 2009
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..
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,403
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 224
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: pure virtual function in constructor

 
0
  #2
Jun 15th, 2009
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()
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,681
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: pure virtual function in constructor

 
2
  #3
Jun 15th, 2009
Post your code, please.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 16
Reputation: anilopo is an unknown quantity at this point 
Solved Threads: 0
anilopo anilopo is offline Offline
Newbie Poster

Re: pure virtual function in constructor

 
0
  #4
Jun 15th, 2009
Originally Posted by Narue View Post
Post your code, please.
it is pretty long, but the idea:
  1. class Product{
  2. ...
  3. private:
  4. int price;
  5. virtual int calc () = 0;
  6.  
  7. public:
  8. Product (...);
  9. };
  10.  
  11. Product :: Product (...)
  12. {
  13. price = calc ();
  14. }
in all the grandsons the calc function is implemented.


ShawnCplus:
i'm not sure i understand what u r saying, but this pure virtual function have an implementation in the grandsons (although not sons - it supposed to b ok).
Last edited by Narue; Jun 15th, 2009 at 1:15 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: pure virtual function in constructor

 
0
  #5
Jun 15th, 2009
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:

  1. Product :: Product (...)
  2. {
  3. this->price = this->calc ();
  4. }

And since "this" in that context is a Product*, you can't call calc().
Last edited by Clockowl; Jun 15th, 2009 at 1:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,681
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: pure virtual function in constructor

 
2
  #6
Jun 15th, 2009
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 16
Reputation: anilopo is an unknown quantity at this point 
Solved Threads: 0
anilopo anilopo is offline Offline
Newbie Poster

Re: pure virtual function in constructor

 
0
  #7
Jun 15th, 2009
Originally Posted by Clockowl View Post
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:

  1. Product :: Product (...)
  2. {
  3. this->price = this->calc ();
  4. }

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)
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 16
Reputation: anilopo is an unknown quantity at this point 
Solved Threads: 0
anilopo anilopo is offline Offline
Newbie Poster

Re: pure virtual function in constructor

 
0
  #8
Jun 15th, 2009
Originally Posted by Narue View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,681
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: pure virtual function in constructor

 
2
  #9
Jun 15th, 2009
>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:
  1. class Product {
  2. int _price;
  3. public:
  4. Product(int price): _price(price) {}
  5. int price() { return _price; }
  6. };
  7.  
  8. class MyProduct: public Product {
  9. int _quantity;
  10. public:
  11. MyProduct(int quantity)
  12. : Product(calc()), _quantity(quantity)
  13. {}
  14. int quantity() { return _quantity; }
  15. private:
  16. int calc() { return 12345; }
  17. };
  18.  
  19. #include <iostream>
  20.  
  21. int main()
  22. {
  23. Product *p = new MyProduct(10);
  24.  
  25. std::cout<< p->price() <<'\n'<< ((MyProduct*)p)->quantity() <<'\n';
  26. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: pure virtual function in constructor

 
0
  #10
Jun 15th, 2009
Hey, sorry to go offtopic anilopo, but Narue reminded me,

If I have a class like this:

  1. class X{
  2. public:
  3. X(int y) : y(y) {}
  4.  
  5. private:
  6. int y;
  7. }

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.
Last edited by Clockowl; Jun 15th, 2009 at 3:20 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC