c++ - Error in accessing derived class function from base class pointer -


I am unable to use the class function derived from base class pointer. This is my code

  #include & lt; Iostream & gt; Class base {public: zero funny} (cout  Fun (); Error static_cast & lt; der * & gt; (pb) - & gt; Fun () returns; // Error here ((* * pb *) -> Funny () OK Return 0;}   

Do I know if you have many derivatives from base classes There are classes and assume that you do not need to create many derived class objects, you create a base class pointer, accessing the class function derived from the base class pointers here is a subject of interest, I have also heard that < strong> base class pointer is a generic pointer , so it can contain an derived class object, so why can not we do it directly

  der d1; Punjab = & amp; d1; Punjab & gt; Fun ();   

Derivative class fun () should be called.

But why does it give a compile time error?

your base class funny () function virtualize

Try it:

  Namespace using CSS; # Include & lt; Iostream & gt; Square base {public: virtual zeros fun () {cout & lt; & Lt; "Base class"; }}; Class Der: Public Base {Public: Zero Funny} (cout & lt; "derivative class";}}; Int main () {base * pb = new der (); Punjab> Fun (); Return 0;}    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -