943,948 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2418
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 15th, 2009
0

pure virtual function in constructor

Expand Post »
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..
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
anilopo is offline Offline
29 posts
since Aug 2008
Jun 15th, 2009
0

Re: pure virtual function in constructor

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()
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jun 15th, 2009
2

Re: pure virtual function in constructor

Post your code, please.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 15th, 2009
0

Re: pure virtual function in constructor

Click to Expand / Collapse  Quote originally posted by Narue ...
Post your code, please.
it is pretty long, but the idea:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Light Poster
anilopo is offline Offline
29 posts
since Aug 2008
Jun 15th, 2009
0

Re: pure virtual function in constructor

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:

cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Jun 15th, 2009
2

Re: pure virtual function in constructor

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 15th, 2009
0

Re: pure virtual function in constructor

Click to Expand / Collapse  Quote originally posted by Clockowl ...
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:

cpp Syntax (Toggle Plain Text)
  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)
Reputation Points: 10
Solved Threads: 0
Light Poster
anilopo is offline Offline
29 posts
since Aug 2008
Jun 15th, 2009
0

Re: pure virtual function in constructor

Click to Expand / Collapse  Quote originally posted by Narue ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
anilopo is offline Offline
29 posts
since Aug 2008
Jun 15th, 2009
2

Re: pure virtual function in constructor

>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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 15th, 2009
0

Re: pure virtual function in constructor

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

If I have a class like this:

cpp Syntax (Toggle Plain Text)
  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.
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Creating 2 dimentional graphs
Next Thread in C++ Forum Timeline: need help with calculations





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC