How do you call a derived function from an array of the base class?  Ex:  #include &lt;iostream&gt; #include &lt;string&gt;  class a{  public:     virtual void prnt(){         std::cout&lt;&lt;"a"&lt;&lt;std::endl;     } };  class b: public a{  public:     virtual void prnt(){         std::cout&lt;&lt;"B"&lt;&lt;std::endl;     } };  int main() {     a array[3];     array[0] = a();     array[1] = b();     array[2] = a();      array[1].prnt(); }   The output for this example is a.  is there anyway to fix this?
664
Someone mentioned it in the IRC as the slicing problem.
94655
I was experimenting with C++ and found the below code as very strange.   class Foo{ public:     virtual void say_virtual_hi(){         std::cout &lt;&lt; "Virtual Hi";     }      void say_hi()     {         std::cout &lt;&lt; "Hi";     } };  int main(int argc, char** argv) {     Foo* foo = 0;     foo-&gt;say_hi(); // works well     foo-&gt;say_virtual_hi(); // will crash the app     return 0; }   I know that the virtual method call crashes because it requires a vtable lookup and can only work with valid objects.   I have the following questions    How does the non virtual method say_hi work on a NULL pointer? Where does the object foo get allocated?   Any thoughts?
3652
How can I dynamically invoke a class method in PHP? The class method is not static. It appears that   call_user_func(...)   only works with static functions?  Thanks.
341
Quote from :     The only portable way of using templates at the moment is to implement them in header files by using inline functions.   Why is this?  (Clarification: header files are not the only portable solution. But they are the most convenient portable solution.)
7460
Is it possible to call the base method from a prototype method in JavaScript if it's been overridden?  MyClass = function(name){     this.name = name;     this.do = function() {         //do somthing      } };  MyClass.prototype.do = function() {       if (this.name === 'something') {         //do something new     } else {         //CALL BASE METHOD     } };
93400
What is the order in which the destructors and the constructors are called in C++? Using the examples of some Base classes and Derived Classes
111702
Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?.  I have tried it and it creates a run-time error.
15076
I have code like this:  class RetInterface {...}  class Ret1: public RetInterface {...}  class AInterface {   public:      virtual boost::shared_ptr&lt;RetInterface&gt; get_r() const = 0;      ... };  class A1: public AInterface {   public:      boost::shared_ptr&lt;Ret1&gt; get_r() const {...}      ... };   This code does not compile.  In visual studio it raises     C2555: overriding virtual function return type differs and is not   covariant   If I do not use boost::shared_ptr but return raw pointers, the code compiles (I understand this is due to  in C++). I can see the problem is because boost::shared_ptr of Ret1 is not derived from boost::shared_ptr of RetInterface. But I want to return boost::shared_ptr of Ret1 for use in other classes, else I must cast the returned value after the return.      Am I doing something wrong?  If not, why is the language like this - it should be extensible to handle conversion between smart pointers in this scenario? Is there a desirable workaround?
106456
Do you have to pass delete the same pointer that was returned by new, or can you pass it a pointer to one of the classes base types?  For example:  class Base { public:     virtual ~Base();     ... };  class IFoo { public:     virtual ~IFoo() {}     virtual void DoSomething() = 0; };  class Bar : public Base, public IFoo { public:     virtual ~Bar();     void DoSomething();     ... };  Bar * pBar = new Bar; IFoo * pFoo = pBar; delete pFoo;   Of course this is greatly simplified.  What I really want to do is create a container full of boost::shared_ptr and pass it to some code that will remove it from the container when it is finished. This code will know nothing of the implementation of Bar or Base, and will rely on the implied delete operator in the shared_ptr destructor to do the right thing.  Can this possibly work? My intuition says no, since the pointers will not have the same address.  On the other hand, a dynamic_cast&lt;Bar*> should work, so somewhere the compiler is storing enough information to figure it out.  Thanks for the help, everybody who answered and commented. I already knew the importance of virtual destructors, as shown in my example; after seeing the answer I gave it a little thought, and realized the whole reason for a virtual destructor is this exact scenario. Thus it had to work. I was thrown by the absence of a visible means of converting the pointer back to the original. A little more thinking led me to believe there was an invisible means, and I theorized that the destructor was returning the true pointer for delete to release. Investigating the compiled code from Microsoft VC++ confirmed my suspicion when I saw this line in ~Base:  mov eax, DWORD PTR _this$[ebp]   Tracing the assembler revealed that this was the pointer being passed to the delete function. Mystery solved.  I've fixed the example to add the virtual destructor to IFoo, it was a simple oversight. Thanks again to everyone who pointed it out.
21871
I am trying to pass a reference to a two-dimensional array to a function in C++. I know the size of both dimensions at compile time. Here is what I have right now:  const int board_width = 80; const int board_height = 80; void do_something(int[board_width][board_height]&amp; array);  //function prototype   But this doesn't work. I get this error from g++:  error: expected ‘,’ or ‘...’ before ‘*’ token   What does this error mean, and how can I fix it?
5309
How do I find out a name of class that created an instance of an object in Python if the function I am doing this from is the base class of which the class of the instance has been derived?  Was thinking maybe  might have helped me out here, but it doesn't seem to give me what I want. And short of parsing the __class__ member, I'm not sure how to get at this information.
81430
Does every object of virtual class have a pointer to vtable?  Or only the object of base class with virtual function has it?  Where did the vtable stored? code section or data section of process?
108226
I'm trying to write a function that formats every (string) member/variable in an object, for example with a callback function. The variable names are unknown to me, so it must work with objects of all classes.  How can I achieve something similar to array_map or array_walk with objects?
52491
C++ does not have native support for lazy evaluation (as Haskell does).  I'm wondering if it is possible to implement lazy evaluation in C++ in a reasonable manner. If yes, how would you do it?  EDIT: I like Konrad Rudolph's answer.  I'm wondering if it's possible to implement it in a more generic fashion, for example by using a parametrized class lazy that essentially works for T the way matrix_add works for matrix.  Any operation on T would return lazy instead. The only problem is to store the arguments and operation code inside lazy itself. Can anyone see how to improve this?
93369
I'm trying to figure out how to make a function that can take an array as a parameter and sort it. I think it is done with positional variables, but I'm not sure.
70551
I know it is a good practice to declare virtual destructors for base classes in C++, but is it always important to declare virtual destructors even for abstract classes that function as interfaces? Please provide some reasons and examples why.
51439
How do you do "inline functions" in C#? I don't think I understand the concept. Are they like anonymous methods? Like lambda functions?  Note: The answers almost entirely deal with the ability to , i.e. "a manual or compiler optimization that replaces a function call site with the body of the callee." If you are interested in , see  or .