inheritance and headers

guys i remember i have read that inheritance slows down the process.

because
a_class -> b_class -> c_class ...
it goes thorough many classes to find a function or a value. people advices not to do it more than a few times. correct me if i m wrong pls

and also there is something that we call using headers. what if instead of inheritance we are using headers, would it slow down the process also??

i mean which one is faster? and arent both the same when we use headers or used inheritance. both does same(???do they) a class calls another

i believe i do not totally understand both yet.
Last edited on
Headers and inheritance don't have anything to do with one another.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//Inheritance
class Base{};
class Derived : public Base{};

//Headers
//MyHeader.h
#ifndef MYHEADER_H
#define MYHEADER_H
int funky();
struct boy_oh_boy{
   int I;
};
#endif

//MySource.cpp
#include "MyHeader.h"
int funky(){return 34;}

//main.cpp
#include "MyHeader.h"
#include <iostream>

int main(){
   boy_oh_boy bob;
   bob.i = 55;
   std::cout << funky() << '\n' << bob.i << std::endl;
   return 0;
}


I don't know if inheriting for the 100th time would really affect performance that much. The only slowdown I know of is from the vtable, which increases overhead cost.
You can look at the inheritance tree of iostream's classes and see that they make heavy use of inheritance.
Last edited on
When using inheiritance with virtual functions, there is a table of pointers to the virtual functions (vtable) for each class. When calling a virtual funtion, the call is made through the pointer in the vtable. The cost to call a function through a pointer is insignificant.

I don't understand your question about headers. Are you referring to header files?
Header files do not contain executable code, so there is nothing to slow down.
maybe i should just forget about my questions other than "does inheritance slow down the process".
i believe i will be able to ask a more proper question in time
Topic archived. No new replies allowed.