Let's consider the following code snippet:  struct A { void f(); }; struct B : virtual A {}; struct C : virtual A {}; struct D : B, C {};   What can I say? that D has two different member functions B::A::f and C::B::f which are called over a same object? or they are just aliases of a same member function?  For example, for the non-virtual case,   struct A { void f(); }; struct B : A {}; struct C : A {}; struct D : B, C {};   D has two different member functions B::A::f and C::A::f because they just have different names, so, calling D().f() is an ambigüity because I have to specify to which member I want to call.  However, in the virtual inhericante case, that isn't required anymore because calling by one path or another resolves to call the function over a same object, and I don't know anymore if, in the virtual inheritance case, I have two different member functions which resolves or are executed over a same object, or they are just aliases of a same member function.  Anyway, from a practical point of view that difference doesn't matter, but from a more formal point of view I'm not sure about what to say.
78578
I want to know what a "virtual base class" is and what it means.  Let me show an example:  class Foo { public:     void DoSomething() { /* ... */ } };  class Bar : public virtual Foo { public:     void DoSpecific() { /* ... */ } };
34763
How do I call the parent function from a derived class using C++? For example, I have a class called parent, and a class called child which is derived from parent. Within  each class there is a print function.  In the definition of the child's print function I would like to make a call to the parents print function.  How would I go about doing this?
33281
In terms of Java, when someone asks:      what is polymorphism?   Would overloading or overriding be an acceptable answer?  I think there is a bit more to it than that.   IF you had a abstract base class that defined a method with no implementation, and you defined that method in the sub class, is that still overridding?  I think overloading is not the right answer for sure.
21478
I have a c++ header file containing a class. I want to use this class in several projects, bu I don't want to create a separate library for it, so I'm putting both methods declarations and definitions in the header file:     // example.h #ifndef EXAMPLE_H_ #define EXAMPLE_H_ namespace test_ns{  class TestClass{ public:     void testMethod(); };  void TestClass::testMethod(){     // some code here... }  } // end namespace test_ns #endif   If inside the same project I include this header from more than one cpp file, I get an error saying "multiple definition of test_ns::TestClass::testMethod()", while if I put the method definition inside the class body this does not happen:  // example.h #ifndef EXAMPLE_H_ #define EXAMPLE_H_ namespace test_ns{  class TestClass{ public:     void testMethod(){         // some code here...     } };  } // end namespace test_ns #endif   Since the class is defined inside a namespace, shouldn't the two forms be equivalent? Why is the method considered to be defined twice in the first case?
14535
I am looking for the definition of when I am allowed to do forward declaration of a class in another class's header file:  Am I allowed to do it for a base class, for a class held as a member, for a class passed to member function by reference, etc. ?
20628
I would like to clarify the differences between by value and by reference.  I drew a picture     So, for passing by value,  a copy of an identical object is created with a different reference, and the local variable is assigned the new reference, so to point to the new copy  How to understand the words: " If the function modifies that value, the modifications appear also within the scope of the calling function for both passing by value and by reference "  Thanks!
62479
I am doing , and am having a few problems with function notation. I understand the form f(x).  What does the form f: x ↦ y mean? Could you also give one or two examples?  And, if possible, state your source. Thank you.
48897
I have a C++ library that provides various classes for managing data.  I have the source code for the library.  I want to extend the C++ API to support C function calls so that the library can be used with C code and C++ code at the same time.  I'm using GNU tool chain (gcc, glibc, etc), so language and architecture support are not an issue.  Are there any reasons why this is technically not possible?  Are there any gotcha's that I need to watch out for?  Are there resources, example code and/or documentation available regarding this?    Some other things that I have found out:   Use the following to wrap your C++ headers that need to be used by C code.     #ifdef __cplusplus extern "C" {   #endif   //   // Code goes here ...   //   #ifdef __cplusplus   } // extern "C"   #endif    Keep "real" C++ interfaces in separate header files that are not included by C.  Think  here.  Using #ifndef __cplusplus #error stuff helps here to detect any craziness. Careful of C++ identifiers as names in C code Enums varying in size between C and C++ compilers.  Probably not an issue if you're using GNU tool chain, but still, be careful. For structs follow the following form so that C does not get confused.  typedef struct X { ... } X  Then use pointers for passing around C++ objects, they just have to be declared in C as struct X where X is the C++ object.   All of this is courtesy of a friend who's a wizard at C++.
16535
Is there any good example to give the difference between a struct and a union? Basically I know that struct uses all the memory of its member and union uses the largest members memory space. Is there any other OS level difference?
22662
In what scenarios is it better to use a struct vs a class in C++?
72809
When you create an instance of a class with the new operator, memory gets allocated on the heap. When you create an instance of a struct with the new operator where does the memory get allocated, on the heap or on the stack ?
7757
What are the benefits of passing by pointer over passing by reference in C++?  Lately, I have seen a number of examples that chose passing function arguments by pointers instead of passing by reference.  Are there benefits to doing this?  Example:  func(SPRITE *x);   with a call of  func(&amp;mySprite);   vs.  func(SPRITE &amp;x);   with a call of  func(mySprite);
46005
If I have a source.c file with a struct:  struct a {      int i;     struct b {         int j;     } };   How can this struct be used in another file (i.e. func.c)?  Should I create a new header file, declare the struct there and include that header in func.c?  Or should I define the whole struct in a header file and include that in both source.c and func.c?  How can the struct be declared extern in both files?  Should I typedef it? If so, how?
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?
50795
I am programming in C++ for many years, still I have doubt about one thing. In many places in other people code I see something like:  void Classx::memberfunction() {     this-&gt;doSomething(); }   If I need to import/use that code, I simply remove the this-> part, and I have never seen anything broken or having some side-effects.  void Classx::memberfunction() {     doSomething(); }   So, do you know of any reason to use such construct?  EDIT: Please note that I'm talking about member functions here, not variables. I understand it can be used when you want to make a distinction between a member variable and function parameter.  EDIT: apparent duplicate:
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?
42622
We all know what virtual functions are in C++, but how are they implemented at a deep level?  Can the vtable be modified or even directly accessed at runtime?  Does the vtable exist for all classes, or only those that have at least one virtual function?   Do abstract classes simply have a NULL for the function pointer of at least one entry?  Does having a single virtual function slow down the whole class? Or only the call to the function that is virtual?  And does the speed get affected if the virtual function is actually overwritten or not, or does this have no effect so long as it is virtual.