So I was looking at LLVM's Call instruction code, and it has a public constructor like so:    static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef&lt;Value *&gt; Args,                           const Twine &amp;NameStr,                           Instruction *InsertBefore = nullptr) {     return new (ComputeNumOperands(Args.size()))         CallInst(Ty, Func, Args, None, NameStr, InsertBefore);   }   Create calls the appropriate private consstructor CallInst, but with an integer which I don't understand.   This is the code for ComputeNumOperands, and you can see that it returns int.     /// Compute the number of operands to allocate.   static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) {     // We need one operand for the called function, plus the input operand     // counts provided.     return 1 + NumArgs + NumBundleInputs;   }   Hence my question: what does new int Constructor() mean?
1103
Has anyone here ever used C++'s "placement new"? If so, what for? It looks to me like it would only be useful on memory-mapped hardware.
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.
14363
What is the best way to initialize a private, static data member in C++?  I tried this in my header file, but it gives me weird linker errors:  class foo {     private:         static int i; };  int foo::i = 0;   I'm guessing this is because I can't initialize a private member from outside the class.  So what's the best way to do this?
57479
Does C++ provide a guarantee for the lifetime of a temporary variable that is created within a function call but not used as a parameter?  Here's an example class:  class StringBuffer { public:     StringBuffer(std::string &amp; str) : m_str(str)     {         m_buffer.push_back(0);     }     ~StringBuffer()     {         m_str = &amp;m_buffer[0];     }     char * Size(int maxlength)     {         m_buffer.resize(maxlength + 1, 0);         return &amp;m_buffer[0];     } private:     std::string &amp; m_str;     std::vector&lt;char&gt; m_buffer; };   And here's how you would use it:  // this is from a crusty old API that can't be changed void GetString(char * str, int maxlength);  std::string mystring; GetString(StringBuffer(mystring).Size(MAXLEN), MAXLEN);   When will the destructor for the temporary StringBuffer object get called?  Is it:   Before the call to GetString? After GetString returns? Compiler dependent?   I know that C++ guarantees that a local temporary variable will be valid as long as there's a reference to it - does this apply to parent objects when there's a reference to a member variable?  Thanks.
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?
81621
If I write the following class:  public class Example {        int j;       int k;        public Example(int j, int k) {            j = j;            k = k;       }        public static void main(String[] args) {            Example exm = new Example(1,2);            System.out.println(exm.j);            System.out.println(exm.k);       }  }   The program compiles, but when I run the program, the main method will print out two 0s. I know that in order to say that I want to initialize the instance variables in the constructor I have to write:  this.j = j; this.k = k;   But if I don't write it, then which variable is evaluated (or considered) in the constructor (on the left and on the write hand side of the expressions)? Is is the argument or the instance variable? Does it make a difference?  Are there other cases where the use of this is obligatory?
87888
I was reading some Java recently and came across something (an idiom?) new to me: in the program, classes with multiple constructors would also always include a blank constructor. For example:   public class Genotype {   private boolean bits[];   private int rating;   private int length;   private Random random;    public Genotype() {              //  &lt;= THIS is the bandit, this one right here     random = new Random();   }    /* creates a Random genetoype */   public Genotype(int length, Random r) {     random = r;     this.length = length;     bits = new boolean[length];      for(int i=0;i&lt;length;i++) {         bits[i] =random.nextBoolean();     }   }    /* copy constructor */   public Genotype(Genotype g,Random r) {     random = r;     bits = new boolean[g.length];     rating = g.rating;     length = g.length;      for(int i=0;i&lt;length;i++) {         bits[i] = g.bits[i];     }    } }   The first constructor doesn't seem to be a "real" constructor, it seems as though in every case one of the other constructors will be used. So why is that constructor defined at all?
12281
I entered this statement in JSLint:  var number = new Number(3);   And received the following message:     Do not use Number as a constructor.   Why is that? The statement is creating a number object, not a primitive value, so I don't see why using new is a problem.  EDIT: Thanks for all the responses. They've got me thinking further, so I posted a follow-up question .
5040
I'm using an API that requires me to pass a function pointer as a callback. I'm trying to use this API from my class but I'm getting compilation errors.   Here is what I did from my constructor:  m_cRedundencyManager-&gt;Init(this-&gt;RedundencyManagerCallBack);   This doesn't compile - I get the following error:      Error  8 error C3867: 'CLoggersInfra::RedundencyManagerCallBack': function call missing argument list; use '&amp;CLoggersInfra::RedundencyManagerCallBack' to create a pointer to member   I tried the suggestion to use &amp;CLoggersInfra::RedundencyManagerCallBack - didn't work for me.   Any suggestions/explanation for this??  I'm using VS2008.  Thanks!!
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 ?
8697
While looking at online code samples, I have sometimes come across an assignment of a String constant to a String object via the use of the new operator.  For example:  String s; ... s = new String("Hello World");   This, of course, compared to  s = "Hello World";   I'm not familiar with this syntax and have no idea what the purpose or effect would be.  Since String constants typically get stored in the constant pool and then in whatever representation the JVM has for dealing with String constants, would anything even be allocated on the heap?
113560
I expected A::~A() to be called in this program, but it isn't:  #include &lt;iostream&gt;  struct A {   ~A() { std::cout &lt;&lt; "~A()" &lt;&lt; std::endl; } };  void f() {   A a;   throw "spam"; }  int main() { f(); }   However, if I change last line to   int main() try { f(); } catch (...) { throw; }   then A::~A() is called.  I am compiling with "Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86" from Visual Studio 2005. Command line is cl /EHa my.cpp.  Is compiler right as usual? What does standard say on this matter?
99037
I see this from time to time and want to know what it is. I did try google, but its filtering out the characters from the search. I have a few books that don't reference it either.   FWIW, I remember in pascal that is was the assignment operator.   Can anybody point me to the MSDN or similar page?
29551
I've heard that the static_cast function should be preferred to C-style or simple function-style casting.  Is this true?  Why?
30179
In C the following horror is valid:  myFunc() {   return 42;  // return type defaults to int. }   But, what about in C++? I can't find a reference to it either way...  My compiler (Codegear C++Builder 2007) currently accepts it without warning, but I've had comments that this is an error in C++.
118657
When {0} is used to initialize an object, what does it mean? I can't find any references to {0} anywhere, and because of the curly braces Google searches are not helpful.  Example code:  SHELLEXECUTEINFO sexi = {0}; // what does this do? sexi.cbSize = sizeof(SHELLEXECUTEINFO); sexi.hwnd = NULL; sexi.fMask = SEE_MASK_NOCLOSEPROCESS; sexi.lpFile = lpFile.c_str(); sexi.lpParameters = args; sexi.nShow = nShow;  if(ShellExecuteEx(&amp;sexi)) {     DWORD wait = WaitForSingleObject(sexi.hProcess, INFINITE);     if(wait == WAIT_OBJECT_0)         GetExitCodeProcess(sexi.hProcess, &amp;returnCode); }   Without it, the above code will crash on runtime.
53451
I have been noticing __construct a lot with classes. I did a little reading and surfing the web, but I couldn't find an explanation I could understand. I am just beginning with OOP.  I was wondering if someone could give me a general idea of what it is, and then a simple example of how it is used with PHP?