| | |
pure virtual function in constructor
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2008
Posts: 16
Reputation:
Solved Threads: 0
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..
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..
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
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.
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.
•
•
Join Date: Aug 2008
Posts: 16
Reputation:
Solved Threads: 0
it is pretty long, but the idea:
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).
C++ Syntax (Toggle Plain Text)
class Product{ ... private: int price; virtual int calc () = 0; public: Product (...); }; Product :: Product (...) { price = calc (); }
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
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:
And since "this" in that context is a Product*, you can't call calc().

What you're doing in the ctor is basically:
cpp Syntax (Toggle Plain Text)
Product :: Product (...) { this->price = this->calc (); }
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.
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.
•
•
Join Date: Aug 2008
Posts: 16
Reputation:
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:
cpp Syntax (Toggle Plain Text)
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)
•
•
Join Date: Aug 2008
Posts: 16
Reputation:
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.
>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:
>"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)
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'; }
I'm here to prove you wrong.
Hey, sorry to go offtopic anilopo, but Narue reminded me,
If I have a class like this:
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.
If I have a class like this:
cpp Syntax (Toggle Plain Text)
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.
Last edited by Clockowl; Jun 15th, 2009 at 3:20 pm.
![]() |
Similar Threads
- Pure Virtual Functions. beginner help sought. (C++)
- Runtime Error - R 6025 - pure virtual function call (Windows NT / 2000 / XP)
- Vector and virtual function questions (C++)
- pure virtual destructors (C)
- why won't my virtual area function work? (C++)
Other Threads in the C++ Forum
- Previous Thread: Creating 2 dimentional graphs
- Next Thread: need help with calculations
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






