I have encountered a piece of code with method being exposed via public interface while the implementation is private. I'm not sure what should be the expected behavior. Simplified example:  #include &lt;iostream&gt;  class Interface { public:     virtual ~Interface() {}     virtual void myIfMethod() = 0; };  class Derived : public Interface { private:     void myIfMethod(){std::cout &lt;&lt; "private method invoked via public interface" &lt;&lt; std::endl;} };  int main() {     Interface* myObj = new Derived;     myObj-&gt;myIfMethod();     delete myObj;     return 0; }   This sample compiles and executes without a warning:   Is this a correct and well-defined behavior? And if so, why?  Note, the question isn't about private interface method with public implementation (there are multiple such questions on SO) but the other way around.
6477
Is there is any reason to make the permissions on an overridden C++ virtual function different from the base class?  Is there any danger in doing so?  For example:  class base {     public:         virtual int foo(double) = 0; }  class child : public base {     private:         virtual int foo(double); }   The  says that it is a bad idea, but doesn't say why.  I have seen this idiom in some code and I believe that the author was attempting to make the class final, based on an assumption that it is not possible to override a private member function.  However,  shows an example of overriding private functions.  Of course  recommends against doing so.  My concrete questions:   Is there any technical problem with using a different permission for virtual methods in derived classes vs base class? Is there any legitimate reason to do so?
32521
I came across this strange code snippet which compiles fine:  class Car {     public:     int speed; };  int main() {     int Car::*pSpeed = &amp;Car::speed;     return 0; }   Why does C++ have this pointer to a non-static data member of a class? What is the use of this strange pointer in real code?
77597
I got this question when I received a code review comment saying virtual functions need not be inline.   I thought inline virtual functions could come in handy in scenarios where functions are called on objects directly. But the counter-argument came to my mind is -- why would one want to define virtual and then use objects to call methods?  Is it best not to use inline virtual functions, since they're almost never expanded anyway?  Code snippet I used for analysis:  class Temp { public:      virtual ~Temp()     {     }     virtual void myVirtualFunction() const     {         cout&lt;&lt;"Temp::myVirtualFunction"&lt;&lt;endl;     }  };  class TempDerived : public Temp { public:      void myVirtualFunction() const     {         cout&lt;&lt;"TempDerived::myVirtualFunction"&lt;&lt;endl;     }  };  int main(void)  {     TempDerived aDerivedObj;     //Compiler thinks it's safe to expand the virtual functions     aDerivedObj.myVirtualFunction();      //type of object Temp points to is always known;     //does compiler still expand virtual functions?     //I doubt compiler would be this much intelligent!     Temp* pTemp = &amp;aDerivedObj;     pTemp-&gt;myVirtualFunction();      return 0; }
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?
39452
Why was C# designed this way?  As I understand it, an interface only describes behaviour, and serves the purpose of describing a contractual obligation for classes implementing the interface that certain behaviour is implemented.  If classes wish to implement that behavour in a shared method, why shouldn't they?  Here is an example of what I have in mind:  // These items will be displayed in a list on the screen. public interface IListItem {   string ScreenName();   ... }  public class Animal: IListItem {     // All animals will be called "Animal".     public static string ScreenName() {         return "Animal";     } .... }  public class Person: IListItem {      private string name;      // All persons will be called by their individual names.     public string ScreenName() {         return name;     }      ....   }
24042
To make a JavaScript class with a public method I'd do something like:  function Restaurant() {}  Restaurant.prototype.buy_food = function(){    // something here }  Restaurant.prototype.use_restroom = function(){    // something here }   That way users of my class can:  var restaurant = new Restaurant(); restaurant.buy_food(); restaurant.use_restroom();   How do I create a private method that can be called by the buy_food and use_restroom methods but not externally by users of the class?  In other words, I want my method implementation to be able to do:  Restaurant.prototype.use_restroom = function() {    this.private_stuff(); }   But this shouldn't work:  var r = new Restaurant(); r.private_stuff();   How do I define private_stuff as a private method so both of these hold true?  I've read  a few times but it doesn't seem like "private" methods can be called by public methods and "privileged" methods can be called externally.
34071
Consider this:  public class TestClass {      private String a;     private String b;      public TestClass()     {     a = "initialized";     }      public void doSomething()     {     String c;          a.notify(); // This is fine     b.notify(); // This is fine - but will end in an exception     c.notify(); // "Local variable c may not have been initialised"     }  }   I don't get it. "b" is never initialized but will give the same run-time error as "c", which is a compile-time error. Why the difference between local variables and members?  Edit: making the members private was my initial intention, and the question still stands...
30635
I'm a C++ newbie, but I wasn't able to find the answer to this (most likely trivial) question online.  I am having some trouble compiling some code where two classes include each other.  To begin, should my #include statements go inside or outside of my macros?  In practice, this hasn't seemed to matter.  However, in this particular case, I am having trouble.  Putting the #include statements outside of the macros causes the compiler to recurse and gives me "#include nested too deeply" errors.  This seems to makes sense to me since neither class has been fully defined before #include has been invoked.  However, strangely, when I try to put them inside, I am unable to declare a type of one of the classes, for it is not recognized.  Here is, in essence, what I'm trying to compile:  A.h  #ifndef A_H_ #define A_H_  #include "B.h"  class A {     private:         B b;      public:         A() : b(*this) {} };  #endif /*A_H_*/   B.h  #ifndef B_H_ #define B_H_  #include "A.h"  class B {     private:             A&amp; a;      public:         B(A&amp; a) : a(a) {}  };  #endif /*B_H_*/   main.cpp  #include "A.h"  int main() {     A a; }   If it makes a difference, I am using g++ 4.3.2.  And just to be clear, in general, where should #include statements go?  I have always seen them go outside of the macros, but the scenario I described clearly seems to break this principle.  Thanks to any helpers in advance!  Please allow me to clarify my intent if I have made any silly mistakes!
84974
int fn();  void whatever() {     (void) fn(); }   Is there any reason for casting an unused return value to void, or am I right in thinking it's a complete waste of time?  Follow up:   Well that seems pretty comprehensive.  I suppose it's better than commenting an unused return value since self documenting code is better than comments. Personally, I'll turn these warnings off since it's unnecessary noise.    I'll eat my words if a bug escapes because of it...
48023
Considering "private" is the default access modifier for class Members, why is the keyword even needed?
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:
105996
When I'm debugging, I often have to deal with methods that does not use an intermediate variable to store the return value :     private int myMethod_1()    {       return 12;    }     private int myMethod_2()    {       return someCall( someValue );    }   Well, in order to recreate a bug, I often have to change values on the fly. Here, I could want to see what happens when myMethode_1 return zero. Same thing for myMethod_2.  Is there a way to do that without modifying the code ? What I would like to do, it's to place a breakpoint on the return line (or on the closing bracket) and type-in a new return value.  Note : I'm using Visual Studio 2005 and Visual Studio 2008.  Thank !
50500
A curious thing happens in Java when you use an abstract class to implement an interface: some of the interface's methods can be completely missing (i.e. neither an abstract declaration or an actual implementation is present), but the compiler does not complain.  For example, given the interface:  public interface IAnything {   void m1();   void m2();   void m3(); }   the following abstract class gets merrily compiled without a warning or an error:  public abstract class AbstractThing implements IAnything {   public void m1() {}   public void m3() {} }   Can you explain why?
19900
I have just found a static nested interface in our code-base.  class Foo {     public static interface Bar {         /* snip */     }     /* snip */ }   I have never seen this before. The original developer is out of reach. Therefore I have to ask SO:  What are the semantics behind a static interface? What would change, if I remove the static? Why would anyone do this?
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?
44389
by default is a class:   private ? internal ?  sealed ?
31511
I have read  about how to test private methods. I usually do not test them, because I always thought it's faster to test only public methods that will be called from outside the object. Do you test private methods? Should I always test them?