class Base{  public:     virtual void func2() const{         cout &lt;&lt; "1" &lt;&lt; endl;     }      virtual void func1(Base temp){         cout &lt;&lt; "Class 1" &lt;&lt; endl;         temp.func2();     }  }; class Derived : public Base{ public:      void func2() const{         cout &lt;&lt; "2" &lt;&lt; endl;     }     void func1(Base temp){         cout &lt;&lt; "Class 2" &lt;&lt; endl;         temp.func2();     }  };  int main(){     Base one;     Derived two;      two.func1(two);     cout &lt;&lt; "------------\n";       return 0; }   Output is:  Class 2  1  So my question is: why when I'm giving func1(two) an object of Derived, it is not calling on virtual func2 that outputs 2. The compiler is always choosing func2() that is defined in the Base class.  It does, however, chooses func2 in the Derived class when you call on it, i.e. two.func2(); which outputs 2
664
Someone mentioned it in the IRC as the slicing problem.
37032
I have a question about inheritance in Java.  I have two classes A and B , and class B, inherits from A:  public class A {      public A() {          System.out.println("Hi!");      } }   public class B extends A {      public B() {          System.out.println("Bye!");      }       public static void main(String[] args) {          B b = new B();      } }   When I run program B, the output is:  Hi! Bye!      Question : why the constructor of class A is invoked, when I create and object of class B ?    I know that B inherits everything from A - all instance or class variables, and all methods, and in this sense an object of B has all characteristics of A plus some other characteristics defined in B. However, I didn't know and didn't imagine that when I create an object of type B, the constructor of A is also invoked. So, writing this:  B b = new B();   creates Two objects - one of type B, and one of type A.  This is getting interesting,   can somebody explain why exactly this happens?
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?
73156
I usually never see test for new in C++ and I was wondering why.  Foo *f = new Foo;  // f is assumed as allocated, why usually, nobody test the return of new?
113530
I want to make this specialized w/o changing main. Is it possible to specialize something based on its base class? I hope so.  -edit-  I'll have several classes that inherit from SomeTag. I don't want to write the same specialization for each of them.  class SomeTag {}; class InheritSomeTag : public SomeTag {};  template &lt;class T, class Tag=T&gt; struct MyClass { };  template &lt;class T&gt; struct MyClass&lt;T, SomeTag&gt; {     typedef int isSpecialized; };  int main() {     MyClass&lt;SomeTag&gt;::isSpecialized test1; //ok     MyClass&lt;InheritSomeTag&gt;::isSpecialized test2; //how do i make this specialized w/o changing main()     return 0; }
82117
As Scott Myers wrote, you can take advantage of a relaxation in C++'s type-system to declare clone() to return a pointer to the actual type being declared:  class Base {     virtual Base* clone() const = 0; };  class Derived : public Base {     virtual Derived* clone() const };   The compiler detects that clone() returns an pointer to the type of the object, and allows Derived to override it to return a pointer to derived.  It would desirable to have clone() return a smart pointer that implies transfer of ownership semantics, like the following:  class Base {    virtual std::auto_ptr&lt;Base&gt; clone() const = 0; };  class Derived : public Base {     virtual std::auto_ptr&lt;Derived&gt; clone() const; };   Unfortunately, the relaxation of the conventions does not apply to templated smart pointers, and the compiler will not allow the override.  So, it seems I am left with two options:   Have clone() return a "dumb" pointer, and document that clients are responsible for disposing of it. Have clone() return a smart base pointer, and have clients use dynamic_cast to save them to a Derived pointer if they need it.   Is one of these approaches preferred?  Or is there a way for me to eat my transfer of ownership semantics and have my strong type safety too?
54427
I have two classes: Action and MyAction. The latter is declared as:  class MyAction extends Action {/* some methods here */}   All I need is method in the Action class (only in it, because there will be a lot of inherited classes, and I don’t want to implement this method in all of them), which will return classname from a static call. Here is what I’m talking about:  Class Action {  function n(){/* something */} }   And when I call it:  MyAction::n(); // it should return "MyAction"   But each declaration in the parent class has access only to the parent class __CLASS__ variable, which has the value “Action”.  Is there any possible way to do this?
107586
Can a class overload methods that also exist in the publicly inherited interface? It seems like this is unambiguous and useful, but compilers (VC, Intel, GCC) all complain, at least by my construction. Below is a toy example. The inherited rebound() function has two clear overloads, yet this will not compile.  If you rename the rebound() method in either class, it works fine, but if they share the same member function name (even though they're overloaded with different argument types!) you get a fatal error of "too few arguments to function call."  The workaround is trivial (I'll just rename the methods) but I'm just trying to understand if this is a C++ restriction (and why it would be).   #include  class Bound { public:   Bound() : x0(0.0), x1(0.0) {};   Bound(double x) : x0(x), x1(x) {};   double width() const {return x1-x0;}   void rebound(const Bound *a, const Bound *b); private:   double x0, x1; };  void Bound::rebound(const Bound *a, const Bound *b) {   if (a && b) {     x0=std::min(a->x0, b->x0);     x1=std::max(a->x1, b->x1);   } }  class Node : public Bound { public:   Node(double x) : Bound(x), left(0), right(0) {};   Node(Node *a, Node *b) : left(a), right(b) {rebound();}   void rebound() { rebound(left, right); } private:   Node *left;   Node *right; };   int main() {   Node A(1.0);   Node B(2.0);   Node C(&A, &B); }
17714
I have a function with the same name, but with different signature in a base and derived classes. When I am trying to use the base class's function in another class that inherits from the derived, I receive an error. See the following code:  class A {     public:     void foo(string s){}; };  class B : public A {     public:     int foo(int i){}; };  class C : public B {     public:     void bar()     {         string s;         foo(s);     } };   I receive the following error from the gcc compiler:  In member function `void C::bar()': no matching function for call to `C::foo(std::string&amp;)' candidates are: int B::foo(int)   If I remove int foo(int i){}; from class B, or if I rename it from foo1, everything works fine.   What's the problem with this?
16650
Can anyone think of any situation to use multiple inheritance? Every case I can think of can be solved by the method operator   AnotherClass() { return this-&gt;something.anotherClass; }
1257
I'm currently refactoring/tidying up some old C code used in a C++ project, and regularly see functions such as:  int f(void)   which I would tend to write as:  int f()   Is there any reason not to replace (void) with () throughout the codebase in order to improve consistency, or is there a subtle difference between the two that I am unaware of?  More specifically, if a virtual member function in C++ is described as:  virtual int f(void)   and a derived class includes a member function:  int f()   is this a valid override?  Additionally, am I likely to encounter any linker problems based on almost identical signatures?
38358
I've been learning C#, and I'm trying to understand lambdas.  In this sample below, it prints out 10 ten times.  class Program {     delegate void Action();     static void Main(string[] args)     {         List&lt;Action&gt; actions = new List&lt;Action&gt;();          for (int i = 0; i &lt; 10; ++i )             actions.Add(()=&gt;Console.WriteLine(i));          foreach (Action a in actions)             a();     } }   Obviously, the generated class behind the lambda is storing a reference or pointer to the int i variable, and is assigning a new value to the same reference every time the loop iterates.  Is there a way to force the lamda to grab a copy instead, like the C++0x syntax  [&amp;](){ ... } // Capture by reference   vs.  [=](){ ... } // Capture copies
8989
Is anyone aware of a language feature or technique in C++ to prevent a child class from over riding a particular method in the parent class?  class Base { public:     bool someGuaranteedResult() { return true; } };  class Child : public Base { public:     bool someGuaranteedResult() { return false; /* Haha I broke things! */ } };   Even though it's not virtual, this is still allowed (at least in the Metrowerks compiler I'm using), all you get is a compile time warning about hiding non-virtual inherited function X.
97448
For example:  public class A : A.B {     public class B { } }   Which generates this error from the compiler:     Circular base class dependency   involving 'A' and 'A.B'   I always figured a nested class behaved just like a regular class except with special rules concerning accessing the outer class's private members, but I guess there's some implicit inheritance occurring between the two classes?
22301
I just came across an interesting situation in JavaScript.  I have a class with a method that defines several objects using object-literal notation.  Inside those objects, the this pointer is being used.  From the behavior of the program, I have deduced that the this pointer is referring to the class on which the method was invoked, and not the object being created by the literal.    This seems arbitrary, though it is the way I would expect it to work.  Is this defined behavior?  Is it cross-browser safe?  Is there any reasoning underlying why it is the way it is beyond "the spec says so" (for instance, is it a consequence of some broader design decision/philosophy)?  Pared-down code example:  // inside class definition, itself an object literal, we have this function: onRender: function() {      this.menuItems = this.menuItems.concat([         {             text: 'Group by Module',             rptletdiv: this         },         {             text: 'Group by Status',             rptletdiv: this         }]);     // etc }
12125
This question was .  Now I'd like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design.  I'll start with an obvious difference:   If you don't specify public: or private:, members of a struct are public by default; members of a class are private by default.   I'm sure there are other differences to be found in the obscure corners of the C++ specification.
41914
Consider:  int testfunc1 (const int a) {   return a; }  int testfunc2 (int const a) {   return a; }   Are these two functions the same in every aspect or is there a difference?  I'm interested in an answer for the C language, but if there is something interesting in the C++ language, I'd like to know as well.