In C++ compiled with -O3, does inheritance without virtuality have a cost in terms of :
- execution time
- memory
If the answer is yes : why ?
As an example : are MyClass1 and MyClass2 equivalent in terms of performance and memory ?

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Of what? Functions are resolved statically, so function calls are the same.
MyClass1‘s constructor will call the constructors of base classes, and its destructor will call destructors of base classes, so for construction & destruction there may be some overhead. Maybe. Some compilers might optimize the calls away.This will be the same, both only have a member
double. Theoretically. Depends on the implementation I guess, as it’s not mandated by the standard, but most commonly there will be no memory overhead.Note that deleting an object
MyClass1through a pointer toDerivedresults in undefined behaviour, because there’s novirtualdestructor.Note 2 inheritance without polymorphism is a code smell. Not saying it’s wrong, but in most cases composition is better.