So I understand the basic concept of passing by value or reference. In this example I'm trying to catch an exception either by value or by reference:  #include &lt;iostream&gt; int main() {     try {         throw std::invalid_argument("nope");     } catch(std::exception &amp;e) {         std::cout &lt;&lt; e.what() &lt;&lt; std::endl;     } }   Outputs: "nope"  #include &lt;iostream&gt; int main() {     try {         throw std::invalid_argument("nope");     } catch(std::exception e) {         std::cout &lt;&lt; e.what() &lt;&lt; std::endl;     } }   (notice the missing &amp;) Outputs: "std::exception"  Shouldn't I get the same output given that the second version "copies" e?
664
Someone mentioned it in the IRC as the slicing problem.
16494
When you pass a collection like list, array to another function in python, does it make a copy of it, or is it just a pointer?
84744
In Ruby, is it possible to pass by reference a parameter with value-type semantics (e.g. a Fixnum)? I'm looking for something similar to C#'s 'ref' keyword.  Example:  def func(x)      x += 1 end  a = 5 func(a)  #this should be something like func(ref a) puts a   #should read '6'   Btw. I know I could just use:  a = func(a)
115337
I have read several places that the difference between c_str() and data() (in STL and other implementations) is that c_str() is always null terminated while data() is not. As far as I have seen in actual implementations, they either do the same or data() calls c_str().  What am I missing here? Which one is more correct to use in which scenarios?
84232
Alright, I think we all agree that what happens with the following code is undefined, depending on what is passed,  void deleteForMe(int* pointer) {      delete[] pointer; }   The pointer could be all sorts of different things, and so performing an unconditional delete[] on it is undefined. However, let's assume that we are indeed passing an array pointer,  int main() {      int* arr = new int[5];      deleteForMe(arr);      return 0; }   My question is, in this case where the pointer is an array, who is it that knows this? I mean, from the language/compiler's point of view, it has no idea whether or not arr is an array pointer versus a pointer to a single int. Heck, it doesn't even know whether arr was dynamically created. Yet, if I do the following instead,  int main() {      int* num = new int(1);      deleteForMe(num);      return 0; }   The OS is smart enough to only delete one int and not go on some type of 'killing spree' by deleting the rest of the memory beyond that point (contrast that with strlen and a non-\0-terminated string -- it will keep going until it hits 0).  So whose job is it to remember these things? Does the OS keep some type of record in the background? (I mean, I realise that I started this post by saying that what happens is undefined, but the fact is, the 'killing spree' scenario doesn't happen, so therefore in the practical world someone is remembering.)
98177
In Java, is there an elegant way to detect if an exception occurred prior to running the finally block? When dealing with "close()" statements, it's common to need exception handling within the finally block. Ideally, we'd want to maintain both exceptions and propagate them up (as both of them may contain useful information). The only way I can think of to do this is to have a variable outside the try-catch-finally scope to save a reference to a thrown exception. Then propagate the "saved" exception up with any that occur in the finally block.  Is there a more elegant way of doing this? Perhaps an API call that will reveal this?  Here's some rough code of what I'm talking about:  Throwable t = null;  try {       stream.write(buffer);  } catch(IOException e) {     t = e;   //Need to save this exception for finally     throw e; } finally {        try {        stream.close();   //may throw exception    } catch(IOException e) {       //Is there something better than saving the exception from the exception block?       if(t!=null) {          //propagate the read exception as the "cause"--not great, but you see what I mean.          throw new IOException("Could not close in finally block: " + e.getMessage(),t);       } else {          throw e;  //just pass it up       }        }//end close }   Obviously, there are a number of other similar kludges that might involve saving the exception as an member variable, returning it from a method, etc... but I'm looking for something a bit more elegant.  Maybe something like Thread.getPendingException() or something similar? For that matter, is there an elegant solution in other languages?  This question actually spawned from comments in  that raised an interesting question.
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;?
95328
Why don't we use "to generate" or "to get"?  E.g.: "External component has thrown an exception", "JavaScript had just thrown an error straight away", "Instances of Error objects are thrown when runtime errors occur".
29529
I am a big fan of letting the compiler do as much work for you as possible.  When writing a simple class the compiler can give you the following for 'free':   A default (empty) constructor A copy constructor A destructor An assignment operator (operator=)   But it cannot seem to give you any comparison operators - such as operator== or operator!=.  For example:  class foo { public:     std::string str_;     int n_; };  foo f1;        // Works foo f2(f1);    // Works foo f3; f3 = f2;       // Works  if (f3 == f2)  // Fails { }  if (f3 != f2)  // Fails { }   Is there a good reason for this?  Why would performing a member-by-member comparison be a problem?  Obviously if the class allocates memory then you'd want to be careful, but for a simple class surely the compiler could do this for you?
36697
I was wondering if there is an iterator in the STL that dereferences the object pointed before returning it. This could be very useful when manipulating containers aggregating pointers. Here's an example of what I would like to be able to do:  #include &lt;vector&gt; #include &lt;iterator&gt; #include &lt;algorithm&gt;  using namespace std;  int main() {   vector&lt;int*&gt; vec;    int i = 1;   int j = 2;   int k = 3;    vec.push_back(&amp;i);   vec.push_back(&amp;j);   vec.push_back(&amp;k);    copy(deref_iterator(vec.begin()),         deref_iterator(vec.end()),         ostream_iterator&lt;int&gt;(cout, " ")); // prints "1 2 3"    return 0; }
37260
In C# and in Java (and possibly other languages as well), variables declared in a "try" block are not in scope in the corresponding "catch" or "finally" blocks.  For example, the following code does not compile:  try {   String s = "test";   // (more code...) } catch {   Console.Out.WriteLine(s);  //Java fans: think "System.out.println" here instead }   In this code, a compile-time error occurs on the reference to s in the catch block, because s is only in scope in the try block.  (In Java, the compile error is "s cannot be resolved"; in C#, it's "The name 's' does not exist in the current context".)  The general solution to this issue seems to be to instead declare variables just before the try block, instead of within the try block:  String s; try {   s = "test";   // (more code...) } catch {   Console.Out.WriteLine(s);  //Java fans: think "System.out.println" here instead }   However, at least to me, (1) this feels like a clunky solution, and (2) it results in the variables having a larger scope than the programmer intended (the entire remainder of the method, instead of only in the context of the try-catch-finally).  My question is, what were/are the rationale(s) behind this language design decision (in Java, in C#, and/or in any other applicable languages)?
31080
Are PHP variables passed by value or by reference?
82090
int i = 4; string text = "Player "; cout &lt;&lt; (text + i);   I'd like it to print Player 4.  The above is obviously wrong but it shows what I'm trying to do here. Is there an easy way to do this or do I have to start adding new includes?
11702
Some days ago I realized that  (as well as ) never throw an IOException when writing, flushing or closing.  Instead it sets an internal flag (trouble=true) when an error occurs. It's not possible to get the exact exception, but only if there was some exception ().  My question is: why would one want to have such behavior? Isn't that bad API design?
2603
I have been bitten by something unexpected recently. I wanted to make something like that:  try :      thing.merge(iterable) # this is an iterable so I add it to the list except TypeError :      thing.append(iterable) # this is not iterable, so I add it   Well, It was working fine until I passed an object inheriting from Exception which was supposed to be added.  Unfortunetly, an Exception is iterable. The following code does not raise any TypeError:  for x in Exception() :     print 1   Does anybody know why?
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?
11842
I know that according to C++ standard in case the new fails to allocate memory it is supposed to throw std::bad_alloc exception. But I have heard that some compilers such as VC6 (or CRT implementation?) do not adhere to it. Is this true ? I am asking this because checking for NULL after each and every new statement makes code look very ugly.