I don't use C++ very often, so probably I'll post dummy question.  I have base class  class BaseObject { public:     template &lt;class T&gt; T* addIntoAutoreleasePool(T*); };   Its implementation:  template &lt;class T&gt; T* BaseObject::addIntoAutoreleasePool(T* argument) {     return argument; }   When I use this method in BaseClass with this code:  BaseObject::BaseObject() {     int a = 5;     this-&gt;addIntoAutoreleasePool&lt;int&gt;(&amp;a); }   it works fine, but if I use this code in child class:  class MazeGame : public BaseObject { public:     Maze* createMaze(); };  Maze* MazeGame::createMaze() {     int a = 5;     this-&gt;addIntoAutoreleasePool&lt;int&gt;(&amp;a); }   compiller complains:  Undefined symbols for architecture x86_64:   "int* BaseObject::addIntoAutoreleasePool&lt;int&gt;(int*)", referenced from:       MazeGame::createMaze() in MazeGame.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)   My IDE is XCode 8. I Have some ideas why it happens:   If I use addIntoAutoreleasePool method in both base and child classes it works fine. So... I have to force methods generation for needed type.(but I don't know how. I hoped that C++ must do it automatically.). And I need this method for "any type" in me future code, custom classes, ints etc. Probably it is just some XCode bug. But any way I have to solve this problem somehow. =(   PS :Anyway, thanks for attention =)
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.)
59937
I am looking for a way to identify primitives types in a template class definition.  I mean, having this class :  template&lt;class T&gt; class A{ void doWork(){    if(T isPrimitiveType())      doSomething();    else      doSomethingElse();  } private: T *t;  };   Is there is any way to "implement" isPrimitiveType().
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.
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.
80212
For many questions the answer seems to be found in "the standard". However, where do we find that? Preferably online.  Googling can sometimes feel futile, again especially for the C standards, since they are drowned in the flood of discussions on programming forums.  To get this started, since these are the ones I am searching for right now, where are there good online resources for:   C89 C99 C11 C++98 C++03 C++11 C++14 C++17
102790
For deployment reasons, I am trying to use IJW to wrap a C# assembly in C++ instead of using a COM Callable Wrapper.    I've done it on other projects, but on this one, I am getting an EEFileLoadException.  Any help would be appreciated!  Managed C++ wrapper code (this is in a DLL):  extern "C" __declspec(dllexport) IMyObject* CreateMyObject(void) {     //this class references c# in the constructor     return new CMyWrapper( ); }  extern "C" __declspec(dllexport)  void DeleteMyObject(IMyObject* pConfigFile) {     delete pConfigFile; }  extern "C" __declspec(dllexport) void TestFunction(void) {     ::MessageBox(NULL, _T("My Message Box"), _T("Test"), MB_OK); }   Test Code (this is an EXE):  typedef void* (*CreateObjectPtr)(); typedef void (*TestFunctionPtr)();  int _tmain testwrapper(int argc, TCHAR* argv[], TCHAR* envp[]) {     HMODULE hModule = ::LoadLibrary(_T("MyWrapper"));     _ASSERT(hModule != NULL);      PVOID pFunc1 = ::GetProcAddress(hModule, "TestFunction");     _ASSERT(pFunc1 != NULL);     TestFunctionPtr pTest = (TestFunctionPtr)pFunc1;      PVOID pFunc2 = ::GetProcAddress(hModule, "CreateMyObject");     _ASSERT(pFunc2 != NULL);     CreateObjectPtr pCreateObjectFunc = (CreateObjectPtr)pFunc2;      (*pTest)();  //this successfully pops up a message box     (*pCreateObjectFunc)();  //this tosses an EEFileLoadException      return 0; }   For what it's worth, the Event Log reports the following: .NET Runtime version 2.0.50727.143 -  Fatal Execution Engine Error (79F97075) (80131506)  Unfortunately, Microsoft has no information on that error.
122587
I am trying to find a generic way of accessing a set of containers. I have a standard vector and list in addition to another custom list.  The custom list defines an iterator;  class Iterator: public std::iterator&lt;std::forward_iterator_tag, T&gt; {     // ... }  Iterator begin() {     return (Iterator(root)); }  Iterator end() {     return (Iterator(NULL)); }   with the appropriate operators overloaded.   Ideally, I would like to do this;  class Foo { public:     Foo() {         std::list&lt;int&gt; x;         std::vector&lt;int&gt; y;         custom_list&lt;int&gt; z;          iter = x.begin(); // OR         iter = y.begin(); // OR         iter = z.begin();          // ...     }; private:     std::iterator&lt;int&gt; iter; };   But obviously these are all iterators of different types. I can assume all the containers are of the same type however.  Is there an elegant way to solve this problem?
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.
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); }
983
I have some template code that I would prefer to have stored in a CPP file instead of inline in the header.  I know this can be done as long as you know which template types will be used.  For example:  .h file  class foo { public:     template &lt;typename T&gt;     void do(const T&amp; t); };   .cpp file  template &lt;typename T&gt; void foo::do(const T&amp; t) {     // Do something with t }  template void foo::do&lt;int&gt;(const int&amp;); template void foo::do&lt;std::string&gt;(const std::string&amp;);   Note the last two lines - the foo::do template function is only used with ints and std::strings, so those definitions mean the app will link.  My question is - is this a nasty hack or will this work with other compilers/linkers? I am only using this code with VS2008 at the moment but will be wanting to port to other environments.
118553
In C++ I can't use a method if I declare it after the calling method.    Does this order matter in other languages like Java or C#?
7396
Why the following example prints "0" and what must change for it to print "1" as I expected ?  #include &lt;iostream&gt; struct base {    virtual const int value() const {       return 0;    }    base() {       std::cout &lt;&lt; value() &lt;&lt; std::endl;    }    virtual ~base() {} };  struct derived : public base {    virtual const int value() const {       return 1;    } };  int main(void) {    derived example; }
17104
Consider these classes.  class Base {    ... };  class Derived : public Base {    ... };   this function  void BaseFoo( std::vector&lt;Base*&gt;vec ) {     ... }   And finally my vector  std::vector&lt;Derived*&gt;derived;   I want to pass derived to function BaseFoo, but the compiler doesn't let me. How do I solve this, without copying the whole vector to a std::vector&lt;Base*&gt;?
71767
I could be totally wrong here, but as I understand it, C++ doesn't really have a native "pointer to member function" type.  I know you can do tricks with Boost and mem_fun etc. But why did the designers of C++ decide not to have a 64-bit pointer containing a pointer to the function and a pointer to the object, for example?  What I mean specifically is a pointer to a member function of a particular object of unknown type.  I.E. something you can use for a callback.  This would be a type which contains two values. The first value being a pointer to the function, and the second value being a pointer to the specific instance of the object.  What I do not mean is a pointer to a general member function of a class. E.G.  int (Fred::*)(char,float)   It would have been so useful and made my life easier.  Hugo
60879
Can anyone recommend any good resources for learning C++ Templates?  Many thanks.
8198
I am writing a small matrix library in C++ for matrix operations. However my compiler complains, where before it did not. This code was left on a shelf for 6 months and in between I upgraded my computer from debian etch to lenny (g++ (Debian 4.3.2-1.1) 4.3.2 ) however I have the same problem on a Ubuntu system with the same g++.  Here is the relevant part of my matrix class:  namespace Math {     class Matrix     {     public:          [...]          friend std::ostream&amp; operator&lt;&lt; (std::ostream&amp; stream, const Matrix&amp; matrix);     } }   And the "implementation":  using namespace Math;  std::ostream&amp; Matrix::operator &lt;&lt;(std::ostream&amp; stream, const Matrix&amp; matrix) {      [...]  }   This is the error given by the compiler:     matrix.cpp:459: error: 'std::ostream&amp;   Math::Matrix::operator&lt;&lt;(std::ostream&amp;,   const Math::Matrix&amp;)' must take   exactly one argument   I'm a bit confused by this error, but then again my C++ has gotten a bit rusty after doing lots of Java those 6 months. :-)
32250
I've looked at , specifically the C++ sample, and fail to recognize the difference between just defining 3 classes, creating instances and calling them, and that example. What I saw was just placing two other classes into the process and cannot see where there would be a benefit. Now I'm sure I'm missing something obvious (wood for the trees) - could someone please explain it using a definitive real-world example?    What I can make from the answers so far, it seems to me to be just a more complex way of doing this:  have an abstract class: MoveAlong with a virtual method: DoIt() have class Car inherit from MoveAlong,       implementing DoIt() { ..start-car-and-drive..} have class HorseCart inherit from MoveAlong,       implementing DoIt() { ..hit-horse..} have class Bicycle inherit from MoveAlong,       implementing DoIt() { ..pedal..} now I can call any function taking MoveAlong as parm  passing any of the three classes and call DoIt Isn't this what Strategy intents? (just simpler?)   [Edit-update] The function I refer to above is replaced with another class in which MoveAlong would be attribute which is set according to need based on the algorithm implemented in this new class. (Similar to what is demonstrated in the accepted answer.)    [Edit-update] Conclusion  The Strategy Pattern has it's uses, but I am a strong believer in KISS, and would tend to more straightforward and less obfuscatory techniques. Mostly since I want to pass on easily maintainable code (and 'cos I'll most likely be the one who have to make the changes!).