Hello i have difficulties understanding inline and static functions. I have 6 files: main.cpp #include &lt;iostream&gt;  #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt;  #include &quot;Data_1.h&quot; #include &quot;Data_2.h&quot;  using namespace std;  int main() { Data_1  Data_1_Test; Data_2 Data_2_Test;   Data_1_Test.Print_Data_1(); Data_2_Test.Print_Data_2();  cout &lt;&lt; &quot;Hello world!&quot; &lt;&lt; endl;  return 0; }  Helper.h #ifndef HELPER_H_INCLUDED #define HELPER_H_INCLUDED  #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt;  void Func() { printf(&quot;Func() called\n&quot;); }    #endif // HELPER_H_INCLUDED  Data_1.h #ifndef DATA_1_H_INCLUDED #define DATA_1_H_INCLUDED  #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &quot;Helper.h&quot;  class Data_1 {  public:  Data_1();   void Print_Data_1();  ~Data_1();     }; #endif // DATA_1_H_INCLUDED  Data_1.cpp #include &quot;Data_1.h&quot;   Data_1::Data_1(){}   void Data_1::Print_Data_1() { printf(&quot;Data 1 called \n&quot;);    Func(); }  Data_1::~Data_1(){}  Data_2.h #ifndef DATA_2_H_INCLUDED #define DATA_2_H_INCLUDED   #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt;  #include &quot;Helper.h&quot;   class Data_2 {   public:   Data_2();   void Print_Data_2();  ~Data_2();    };  #endif // DATA_2_H_INCLUDED  Data_2.cpp #include &quot;Data_2.h&quot;  Data_2::Data_2(){}    void Data_2::Print_Data_2() { printf(&quot;Data 2 called\n&quot;);  Func(); }  Data_2::~Data_2(){}  When compiling i received a error message: C:/Users/MY_NAME/Documents/CodeBlocksProj/C++_Test/Helper.h:9: multiple definition of `Func()'; C:/Users/MY_NAME/Documents/CodeBlocksProj/C++_Test/Helper.h:9: first defined here  So, in Helper.h i decided to make the function Func() inline (L8) inline void Func() { printf(&quot;Func() called\n&quot;); }  After making Func() inline,i tried to compile the code and it compiled successfuly. So, i decided to change the code again,this time i made Func() static static void Func() { printf(&quot;Func() called\n&quot;); }  I tried to compile the code  and it compiled successfully. As you can see,i first tried to compile the code without making  Func() static or inline and the compilation  failed,so  i tried to compile the code  by making Func() inline or static  and the compilation was successful. I have 2 questions: Why the code failed to compile when Func() was   without static or inline. What is static and inline in c++ ?
26363
I've seen the word static used in different places in C code; is this like a static function/class in C# (where the implementation is shared across objects)?
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.
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#?
65547
See also      Not a major issue, just annoying as I don't want my class to ever be instantiated without the particular arguments.  #include &lt;map&gt;  struct MyClass {     MyClass(int t); };  int main() {     std::map&lt;int, MyClass&gt; myMap;     myMap[14] = MyClass(42); }   This gives me the following g++ error:     /usr/include/c++/4.3/bits/stl_map.h:419: error: no matching function for call to ‘MyClass()’   This compiles fine if I add a default constructor; I am certain it's not caused by incorrect syntax.
12470
Why do we need to use:  extern "C" { #include &lt;foo.h&gt; }   Specifically:     When should we use it? What is happening at the compiler/linker level that requires us to use it?   How in terms of compilation/linking does this solve the problems which require us to use it?
4123
I just ran across the following error (and found the solution online, but it's not present in Stack Overflow):     (.gnu.linkonce.[stuff]): undefined   reference to [method] [object   file]:(.gnu.linkonce.[stuff]):   undefined reference to `typeinfo for   [classname]'   Why might one get one of these "undefined reference to typeinfo" linker errors?  (Bonus points if you can explain what's going on behind the scenes.)
58615
Have you ever tried this before?  static void Main(string[] args) {     int x = 10;     x = x++;     Console.WriteLine(x); }   Output: 10.  but for   static void Main(string[] args) {     int x = 10;     x++;     Console.WriteLine(x); }   Output: 11.  Could anyone explain why this?
116603
Why do we need to use int main and not void main in C++?
2689
What is the idiomatic Python equivalent of this C/C++ code?  void foo() {     static int counter = 0;     counter++;     printf("counter is %d\n", counter); }   specifically, how does one implement the static member at the function level, as opposed to the class level? And does placing the function into a class change anything?
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?
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?
9623
In what segment (.BSS, .DATA, other) of an executable file are static variables stored so that they don't have name collision? For example:   foo.c:                         bar.c: static int foo = 1;            static int foo = 10; void fooTest() {               void barTest() {   static int bar = 2;            static int bar = 20;   foo++;                         foo++;   bar++;                         bar++;   printf("%d,%d", foo, bar);     printf("%d, %d", foo, bar); }                              }   If I compile both files and link it to a main that calls fooTest() and barTest repeatedly, the printf statements increment independently.  Makes sense since the foo and bar variables are local to the translation unit.  But where is the storage allocated?  To be clear, the assumption is that you have a toolchain that would output a file in ELF format.  Thus, I believe that there has to be some space reserved in the executable file for those static variables. For discussion purposes, lets assume we use the GCC toolchain.
38239
Presuming that your C++ compiler supports them, is there any particular reason not to use __FILE__, __LINE__ and __FUNCTION__ for logging and debugging purposes?  I'm primarily concerned with giving the user misleading data—for example, reporting the incorrect line number or function as a result of optimization—or taking a performance hit as a result.  Basically, can I trust __FILE__, __LINE__ and __FUNCTION__ to always do the right thing?
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?
79031
As usual, when my brain's messing with something I can't figure out myself, I come to you guys for help :)  This time I've been wondering why stdafx.h works the way it does? To my understanding it does 2 things:   Includes standard headers which we might (?) use and which are rarely changed Work as a compiler-bookmark for when code is no longer precompiled.   Now, these 2 things seems like two very different tasks to me, and I wonder why they didn't do two seperate steps to take care of them? To me it seems reasonable to have a #pragma-command do the bookmarking stuff and to optionally have a header-file a long the lines of windows.h to do the including of often-used headers... Which brings me to my next point: Why are we forced to include often-used headers through stdafx.h? Personally, I'm not aware of any often used headers I use that I'm not already doing my own includes for - but maybe these headers are necessary for .dll generation?  Thx in advance
1488
Can anyone explain why following code won't compile? At least on g++ 4.2.4.  And more interesting, why it will compile when I cast MEMBER to int?  #include &lt;vector&gt;  class Foo {   public:       static const int MEMBER = 1;   };  int main(){       vector&lt;int&gt; v;       v.push_back( Foo::MEMBER );       // undefined reference to `Foo::MEMBER'     v.push_back( (int) Foo::MEMBER ); // OK       return 0; }
22280
I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts i.e.  MyClass *m = (MyClass *)ptr;   all over the place, but there seem to be two other types of casts, and I don't know the difference. What's the difference between the following lines of code?  MyClass *m = (MyClass *)ptr; MyClass *m = static_cast&lt;MyClass *&gt;(ptr); MyClass *m = dynamic_cast&lt;MyClass *&gt;(ptr);