I am a bit confused with the 2 lines below.  MyClass myobj[]; myobj = new MyClass[numberVariable];   i would expect in line 1 something like:  MyClass[] myobj;   But the code works and there is not error.  What is the explanation?
5148
I have recently been thinking about the difference between the two ways of defining an array:   int[] array int array[]   Is there a difference?
18453
Is it more efficient for a class to access member variables or local variables? For example, suppose you have a (callback) method whose sole responsibility is to receive data, perform calculations on it, then pass it off to other classes. Performance-wise, would it make more sense to have a list of member variables that the method populates as it receives data? Or just declare local variables each time the callback method is called?  Assume this method would be called hundreds of times a second...  In case I'm not being clear, here's some quick examples:  // use local variables class thisClass {     public:         void callback( msg&amp; msg )         {             int varA;             double varB;             std::string varC;             varA = msg.getInt();             varB = msg.getDouble();             varC = msg.getString();              // do a bunch of calculations          }  };  // use member variables class thisClass {     public:         void callback( msg&amp; msg )         {              m_varA = msg.getInt();              m_varB = msg.getDouble();              m_varC = msg.getString();               // do a bunch of calculations         }      private:         int m_varA;         double m_varB;         std::string m_varC;  };
83470
Is there a built-in method in Python to get an array of all a class' instance variables? For example, if I have this code:  class hi:   def __init__(self):     self.ii = "foo"     self.kk = "bar"   Is there a way for me to do this:  &gt;&gt;&gt; mystery_method(hi) ["ii", "kk"]   Edit: I originally had asked for class variables erroneously.
114087
Say I've got a class like this:  class Test {   int x;   SomeClass s; }   And I instantiate it like this:  Test* t = new Test;   Is x on the stack, or the heap? What about s?
872
In Java, arrays don't override toString(), so if you try to print one directly, you get the className + '@' + the hex of the  of the array, as defined by Object.toString():  int[] intArray = new int[] {1, 2, 3, 4, 5}; System.out.println(intArray);     // prints something like '[I@3343c8b3'   But usually, we'd actually want something more like [1, 2, 3, 4, 5]. What's the simplest way of doing that? Here are some example inputs and outputs:  // Array of primitives: int[] intArray = new int[] {1, 2, 3, 4, 5}; //output: [1, 2, 3, 4, 5]  // Array of object references: String[] strArray = new String[] {"John", "Mary", "Bob"}; //output: [John, Mary, Bob]
80355
I have this struct:  struct Map {     public int Size;      public Map ( int size )     {         this.Size = size;     }      public override string ToString ( )     {         return String.Format ( "Size: {0}", this.Size );     } }   When using array, it works:  Map [ ] arr = new Map [ 4 ] {     new Map(10),     new Map(20),     new Map(30),     new Map(40)};  arr [ 2 ].Size = 0;   But when using List, it doesn't compile:  List&lt;Map&gt; list = new List&lt;Map&gt; ( ) {     new Map(10),     new Map(20),     new Map(30),     new Map(40)};  list [ 2 ].Size = 0;   Why?
3286
Does the order in which I set properties using the object initializer syntax get executed in the exact same order?  For instance if I do this:  var s = new Person { FirstName = "Micah",                      LastName = "Martin",                      IsLoaded = true                    }   will each property get set in the same order?
34071
Consider this:  public class TestClass {      private String a;     private String b;      public TestClass()     {     a = "initialized";     }      public void doSomething()     {     String c;          a.notify(); // This is fine     b.notify(); // This is fine - but will end in an exception     c.notify(); // "Local variable c may not have been initialised"     }  }   I don't get it. "b" is never initialized but will give the same run-time error as "c", which is a compile-time error. Why the difference between local variables and members?  Edit: making the members private was my initial intention, and the question still stands...
90115
I am trying to create an array of pointers. These pointers will point to a Student object that I created. How do I do it? What I have now is:   Student * db = new Student[5];   But each element in that array is the student object, not a pointer to the student object. Thanks.
107003
I'm working on a sparse matrix class that needs to use an array of LinkedList to store the values of a matrix. Each element of the array (i.e. each LinkedList) represents a row of the matrix. And, each element in the LinkedList array represents a column and the stored value.  In my class, I have a declaration of the array as:  private LinkedList&lt;IntegerNode&gt;[] myMatrix;   And, in my constructor for the SparseMatrix, I try to define:  myMatrix = new LinkedList&lt;IntegerNode&gt;[numRows];   The error I end up getting is     Cannot create a generic array of LinkedList&lt;IntegerNode&gt;.   So, I have two issues with this:   What am I doing wrong, and Why is the type acceptable in the declaration for the array if it can't be created?   IntegerNode is a class that I have created. And, all of my class files are packaged together.
3650
This could be the dumbest question ever asked but I think it is quite confusing for a Java newbie.    Can somebody clarify what is meant by immutable?  Why is a String immutable? What are the advantages/disadvantages of the immutable objects? Why should a mutable object such as StringBuilder be preferred over String and vice-verse?   A nice example (in Java) will be really appreciated.
53471
Now that it's clear , there is an associated concept that I use all the time without knowing what it really means.   I suppose everybody made once a mistake with parenthesis, resulting  in an "object is not callable" exception. What's more, using __init__ and __new__ lead to wonder what this bloody __call__ can be used for.  Could you give me some explanations, including examples with the magic method ?
16650
Can anyone think of any situation to use multiple inheritance? Every case I can think of can be solved by the method operator   AnotherClass() { return this-&gt;something.anotherClass; }
5812
Why does the  exist in modern languages such as C# and Java? Is it purely a self documenting code feature, or does it serve any actual purpose?  For instance the following example:  Class1 obj = new Class1();  Class1 foo() {     return new Class1(); }   Is as easy to read as the more Pythonesque way of writing it:  Class1 obj = Class1();  Class1 foo() {     return Class1(); }   EDIT:  hit the nail on the head with the clarification of the question: Why did they choose this syntax?
45838
If I have the following code:  MyClass pClass = new MyClass(); pClass.MyEvent += MyFunction; pClass = null;   Will pClass be garbage collected? Or will it hang around still firing its events whenever they occur? Will I need to do the following in order to allow garbage collection?  MyClass pClass = new MyClass(); pClass.MyEvent += MyFunction; pClass.MyEvent -= MyFunction; pClass = null;
11352
Duplicate:     Maybe this is a very silly question but I don't understand why I can't declare two methods that have the same signature when they have different return types.  public class MyClass {     private double d = 0;      public double MyMethod()     {         return d;     }      public string MyMethod()     {         return d.ToString();     } }   I get a compile error that states that the class already defines a member with the same parameter types.  (Obviously the way I'm using this in my code isn't as simple as my example code...but I think it gets the idea across.)  Am I missing something concerning OO design that makes what I'm trying to do an OOP anti-pattern? Surely the compiler should be able to determine which method I'm trying to use as long as I specifically tell it which one I want.  Given MyClass myClass = new MyClass(); I would expect the following code to work:  double d = myClass.MyMethod(); string s = myClass.MyMethod();   I would expect the following code to have problems:  var v = myClass.MyMethod();   But even in the case of var it should result in a compile error.  Can anyone see what I'm doing wrong here? I'm more than happy to be corrected. :-)
12175
Is it wrong to write:  class A { public:     virtual ~A() = 0; };   for an abstract base class?  At least that compiles in MSVC... Will it crash at run time?