I have two simple classes. I want with vector show result, but number is not showed. On the other hand, when I try result without vector, result will be show. Can you help me? Thank you.  #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;iterator&gt;  using namespace std;   template&lt;typename T&gt; class One { protected:     T word;     T word2;  public:     One() {word = "0"; word2 = "0";}     One(T w, T w2) {word = w; word2 = w2;}     virtual const void Show() {cout &lt;&lt; word &lt;&lt; endl; cout &lt;&lt; word2 &lt;&lt; endl;} };  template&lt;typename T&gt; class Two : public One&lt;T&gt; { private:     int number; public:     Two() {number = 0;}     Two(T w, T w2, int n) : One(w,w2) {number = n;}     virtual const void Show () {cout &lt;&lt; word &lt;&lt; endl; cout &lt;&lt; word2 &lt;&lt; endl; cout &lt;&lt; number &lt;&lt; endl; } };   int main () {     One&lt;string&gt; *idk;     Two&lt;string&gt; *something = new Two&lt;string&gt;("asd","aa",1);     idk = something;      idk-&gt;Show(); // OK - asd, aa, 1      vector&lt;One&lt;string&gt;&gt; arr;     arr.push_back(*idk);      arr.at(0).Show(); // WRONG - asd,aa     return 0; }
664
Someone mentioned it in the IRC as the slicing problem.
36534
Having recently introduced an overload of a method the application started to fail. Finally tracking it down, the new method is being called where I did not expect it to be.  We had  setValue( const std::wstring&amp; name, const std::wstring&amp; value );  std::wstring avalue( func() ); setValue( L"string", avalue ); std::wstring bvalue( func2() ? L"true", L"false" ); setValue( L"bool", bvalue ); setValue( L"empty", L"" );   It was changed so that when a bool value is stored we use the same strings (internal data storage of strings)  setValue( const std::wstring&amp; name, const std::wstring&amp; value ); setValue( const std::wstring&amp; name, const bool&amp; value );  std::wstring avalue( func() ); setValue( L"string", avalue ); setValue( L"bool", func2() ); setValue( L"empty", L"" ); &lt;&lt; --- this FAILS!?!   The problem with L"" is that it is implicitly casting and previously it was happy to be a std::wstring, but not it prefers to be a bool. The MSVC compiler does not complain, or raise warning, so I'm worried that even if I "fix" the setValue( L"empty", L"" ); to be  setValue( L"empty", std::wstring() );   somebody else may come later and simply use setValue( L"empty", L"" ); and have to track down this issue again.  We thought to use explicit on the method but it is not a valid keyword for this usage. Is there some way to get the compiler to complain about this, or otherwise prevent the issue?  Otherwise I'm thinking to change the name of the method which takes a bool to ensure it can't make an incorrect guess.
13767
What is the correct way of iterating over a vector in C++?  Consider these two code fragments, this one works fine:  for (unsigned i=0; i &lt; polygon.size(); i++) {     sum += polygon[i]; }   and this one:  for (int i=0; i &lt; polygon.size(); i++) {     sum += polygon[i]; }   which generates warning: comparison between signed and unsigned integer expressions.  I'm new in the world of C++, so the unsigned variable looks a bit frightening to me and I know unsigned variables can be dangerous if not used correctly, so - is this correct?
35935
I'm using the 'using' declaration in C++ to add std::string and std::vector to the local namespace (to save typing unnecessary 'std::'s).  using std::string; using std::vector;  class Foo { /*...*/ };   What is the scope on this declaration?  If I do this in a header, will it inject these 'using' declarations into every cpp file that includes the header?
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?
71365
I've been using C++ for a short while, and I've been wondering about the new keyword. Simply, should I be using it, or not?  1) With the new keyword...  MyClass* myClass = new MyClass(); myClass-&gt;MyField = "Hello world!";   2) Without the new keyword...  MyClass myClass; myClass.MyField = "Hello world!";   From an implementation perspective, they don't seem that different (but I'm sure they are)... However, my primary language is C#, and of course the 1st method is what I'm used to.  The difficulty seems to be that method 1 is harder to use with the std C++ classes.  Which method should I use?  Update 1:  I recently used the new keyword for heap memory (or free store) for a large array which was going out of scope (i.e. being returned from a function). Where before I was using the stack, which caused half of the elements to be corrupt outside of scope, switching to heap usage ensured that the elements were in tact. Yay!  Update 2:  A friend of mine recently told me there's a simple rule for using the new keyword; every time you type new, type delete.  Foobar *foobar = new Foobar(); delete foobar; // TODO: Move this to the right place.   This helps to prevent memory leaks, as you always have to put the delete somewhere (i.e. when you cut and paste it to either a destructor or otherwise).
65868
There's a good discussion of Generics and what they really do behind the scenes over at , so we all know that Vector&lt;int[]&gt; is a vector of integer arrays, and HashTable&lt;String, Person&gt; is a table of whose keys are strings and values Persons.  However, what stumps me is the usage of Class&lt;&gt;.  The java class Class is supposed to also take a template name, (or so I'm being told by the yellow underline in eclipse).  I don't understand what I should put in there.  The whole point of the Class object is when you don't fully have the information about an object, for reflection and such.  Why does it make me specify which class the Class object will hold?  I clearly don't know, or I wouldn't be using the Class object, I would use the specific one.
45902
In our C++ course they suggest not to use C++ arrays on new projects anymore. As far as I know Stroustroup himself suggests not to use arrays. But are there significant performance differences?
44510
Can someone post a simple example of starting two (Object Oriented) threads in C++.   I'm looking for actual C++ thread objects that I can extend run methods on (or something similar) as opposed to calling a C-style thread library.  I left out any OS specific requests in the hopes that whoever replied would reply with cross platform libraries to use. I'm just making that explicit now.
44161
When I write C++ code for a class using templates and split the code between a source (CPP) file and a header (H) file, I get a whole lot of "unresolved external symbol" errors when it comes to linking the final executible, despite the object file being correctly built and included in the linking.  What's happening here, and how can I fix it?
24065
I mostly use Java and generics are relatively new. I keep reading that Java made the wrong decision or that .NET has better implementations etc. etc.  So, what are the main differences between C++, C#, Java in generics? Pros/cons of each?
47824
I've seen some examples of C++ using template template parameters (that is templates which take templates as parameters) to do policy-based class design. What other uses does this technique have?
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?
26743
How can I format my output in C++? In other words, what is the C++ equivalent to the use of printf like this:  printf("%05d", zipCode);   I know I could just use printf in C++, but I would prefer the output operator &lt;&lt;.  Would you just use the following?  std::cout &lt;&lt; "ZIP code: " &lt;&lt; sprintf("%05d", zipCode) &lt;&lt; std::endl;
67728
Is there any way of doing parallel assignment in C++? Currently, the below compiles (with warnings)  #include &lt;iostream&gt;   int main() {    int a = 4;   int b = 5;   a, b = b, a;   std::cout &lt;&lt; "a: " &lt;&lt; a &lt;&lt; endl             &lt;&lt; "b: " &lt;&lt; b &lt;&lt; endl;    return 0; }   and prints:  a: 4 b: 5   What I'd like it to print ... if it weren't obvious, is:  a: 5 b: 4   As in, say, ruby, or python.
211
In templates, where and why do I have to put typename and template on dependent names? What exactly are dependent names anyway?    I have the following code:  template &lt;typename T, typename Tail&gt; // Tail will be a UnionNode too. struct UnionNode : public Tail {     // ...     template&lt;typename U&gt; struct inUnion {         // Q: where to add typename/template here?         typedef Tail::inUnion&lt;U&gt; dummy;      };     template&lt; &gt; struct inUnion&lt;T&gt; {     }; }; template &lt;typename T&gt; // For the last node Tn. struct UnionNode&lt;T, void&gt; {     // ...     template&lt;typename U&gt; struct inUnion {         char fail[ -2 + (sizeof(U)%2) ]; // Cannot be instantiated for any U     };     template&lt; &gt; struct inUnion&lt;T&gt; {     }; };   The problem I have is in the typedef Tail::inUnion&lt;U&gt; dummy line. I'm fairly certain that inUnion is a dependent name, and VC++ is quite right in choking on it. I also know that I should be able to add template somewhere to tell the compiler that inUnion is a template-id. But where exactly? And should it then assume that inUnion is a class template, i.e. inUnion&lt;U&gt; names a type and not a function?
114469
I was wondering what could be the size of an object of an empty class. It surely could not be 0 bytes since it should be possible to reference and point to it like any other object. But, how big is such an object?  I used this small program:  #include &lt;iostream&gt; using namespace std;  class Empty {};  int main() {     Empty e;     cerr &lt;&lt; sizeof(e) &lt;&lt; endl;     return 0; }   The output I got on both Visual C++ and Cygwin-g++ compilers was 1 byte! This was a little surprising to me since I was expecting it to be of the size of the machine word (32 bits or 4 bytes).  Can anyone explain why the size of 1 byte? Why not 4 bytes? Is this dependent on compiler or the machine too? Also, can someone give a more cogent reason for why an empty class object will not be of size 0 bytes?