c++ - reference and pointer used for polymorphism? -


this question has answer here:

i studying c++.i having problem in under standing run time polymorphism ? why need use reference / pointer when same work can done creating object of class?

`for eg:

class a{ private:     int p; public:     a(){}   //ctor     void print(){}     void display(){}     void showrecord(){} }  class b:public { private:      int q; public:     b(){}    //ctor     void print(){}       void displayrecord(){} } 

now in above case can access method of b class using object , class method b using scope resolution why use pointer , assign object it?

lets you've 3 different cars. , you've different mechanisms of driving them. driver needn't know underlying engines protocol of how drive car i.e. press pedal acceleration, press pedal brakes, etc.

now driver's perspective, doesn't matter if honda, ford or buick. viewpoint, car. likewise, if you've shed, cars parked, call them car shed. houses cars , isn't bothered make each 1 is. so

std::vector<car*> v; v.push_back(new ferrari()); v.push_back(new honda()); v.push_back(new ford()); 

why need use reference / pointer when same work can done creating object of class?

without pointer or reference, cannot create collection of objects have commonality, different in specific sense. circumvented in strict oop languages java, c#, etc. making objects derive base class called object. c++ multi-paradigm language , programmer free make such decisions appropriate his/her project. way in c++ via pointers of base class. idiom called run-time polymorphism.

for (const auto &this_car : v)      this_car->drive(); 

here irrespective of actual make, vector v able hold car, long base class car part of type. likewise, drive different each make, isn't required known function calls it.

edit:

thanks nijansen pointing out post doesn't answer question: why pointers or references (dynamic types) required run-time polymorphism? says they've used achieve doesn't explain why can't use ordinary (static type) variables.

c++ designed in such way object's type may or may not known @ compile-time type in hand. in circle c; know c of type circle; whereas in shape *p = make_shape(); not know object p pointing to; p's own type shape* pointed-to object's concrete type unknown. know is pointing object derives shape. please not confuse dynamic or automatic memory allocation; same automatic variables e.g. shape *p = &c;. here p in isolation compiler doesn't know concrete type object of, p pointing to.

had written p static (non-pointer, non-reference) type shape p = make_shape(); or shape p = c; happens in slicing i.e. p of concrete type shape , since it's not pointer, it'll copy (shape) part of circle object c it, undesirable.

without knowing underlying type, how call right concrete type's functions? c++ provides virtual functions job. why runtime polymorphism explained virtual methods in c++; while in languages java , c# all methods virtual , all object types references/pointers. in way answer question is, the language designed such 1 needs reference/pointer variables runtime polymorphism.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -