Is there ever an advantage of declaring a struct in C++? Why shouldn't I just make a class consisting only of data members(i.e. no methods)?   Thanks,
22662
In what scenarios is it better to use a struct vs a class in C++?
19841
I've been programming in C++ for a few years, and I've used STL quite a bit and have created my own template classes a few times to see how it's done.  Now I'm trying to integrate templates deeper into my OO design, and a nagging thought keeps coming back to me: They're just a macros, really... You could implement (rather UGLY) auto_ptrs using #defines, if you really wanted to.  This way of thinking about templates helps me understand how my code will actually work, but I feel that I must be missing the point somehow. Macros are meant evil incarnate, yet "template metaprogramming" is all the rage.  So, what ARE the real distinctions? and how can templates avoid the dangers that #define leads you into, like   Inscrutable compiler errors in places where you don't expect them? Code bloat?  Difficulty in tracing code? Setting Debugger Breakpoints?
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
42535
If I have a class as follows     class Example_Class     {        private:          int x;           int y;         public:           Example_Class()           {               x = 8;              y = 9;          }        ~Example_Class()         { }     };   And a struct as follows  struct {    int x;    int y; } example_struct;   Is the structure in memory of the  example_struct simmilar to that in Example_Class  for example if I do the following  struct example_struct foo_struct; Example_Class foo_class = Example_Class();  memcpy(&amp;foo_struct, &amp;foo_class, sizeof(foo_struct));   will foo_struct.x = 8 and foo_struct.y = 9 (ie: the same values as the x,y values in the foo_class) ?  The reason I ask is I have a C++ library (don't want to change it) that is sharing an object with C code and I want to use a struct to represent the object coming from the C++ library. I'm only interested in the attributes of the object.  I know the ideal situation would be to have Example_class wrap arround a common structure between the C and C++ code but it is not going to be easy to change the C++ library in use.
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?
33911
I have been reading through the  and was curious about the  declaration. I personally have never used it, however I am interested in exploring the language.   What is a good example of using friend?    Reading the FAQ a bit longer I like the idea of the &lt;&lt; &gt;&gt; operator overloading and adding as a friend of those classes. However I am not sure how this doesn't break encapsulation. When can these exceptions stay within the strictness that is OOP?
26025
Is it better in C++ to pass by value or pass by constant reference?  I am wondering which is better practice.  I realize that pass by constant reference should provide for better performance in the program because you are not making a copy of the variable.
7208
Is it possible to forward declare an standard container in a header file?  For example, take the following code:  #include &lt;vector&gt;  class Foo { private:     std::vector&lt;int&gt; container_;     ... };   I want to be able to do something like this:  namespace std {     template &lt;typename T&gt; class vector; }  class Foo { private:     std::vector&lt;int&gt; container_;     ... };   Can this be done?
107554
Is it possible in C# to have a Struct with a member variable which is a Class type?  If so, where does the information get stored, on the Stack, the Heap, or both?
97117
Following the discussions here on SO I already read several times the remark that mutable structs are “evil” (like in the answer to this ).  What's the actual problem with mutability and structs in C#?
32028
What is the best way to have an associative array with arbitrary value types for each key in C++?  Currently my plan is to create a "value" class with member variables of the types I will be expecting. For example:  class Value {      int iValue;     Value(int v) { iValue = v; }      std::string sValue;     Value(std::string v) { sValue = v; }      SomeClass *cValue;     Value(SomeClass *v) { cValue = c; }  };  std::map&lt;std::string, Value&gt; table;   A downside with this is you have to know the type when accessing the "Value". i.e.:  table["something"] = Value(5); SomeClass *s = table["something"].cValue;  // broken pointer   Also the more types that are put in Value, the more bloated the array will be.  Any better suggestions?
115971
In the context of C++ (not that it matters):  class Foo{     private:         int x[100];     public:         Foo(); }   What I've learnt tells me that if you create an instance of Foo like so:  Foo bar = new Foo();   Then the array x is allocated on the heap, but if you created an instance of Foo like so:  Foo bar;   Then it's created on the stack.  I can't find resources online to confirm this.
5014
I know this is a really basic question, but I've just started with some basic C++ programming after coding a few projects with high-level languages.   Basically I have three questions:   Why use pointers over normal variables? When and where should I use pointers? How do you use pointers with arrays?
54456
Does anyone know why typedefs of class names don't work like class names for the friend declaration?  class A { public: };  class B : public A { public:    typedef A SUPERCLASS; };  typedef A X;  class C { public:    friend class A;             // OK    friend class X;             // fails    friend class B::SUPERCLASS; // fails };
35935
I'm using the 'using' declaration in C++ to add std::string and std::vector to the local namespace (to save typing unnecessary 'std::'s).  using std::string; using std::vector;  class Foo { /*...*/ };   What is the scope on this declaration?  If I do this in a header, will it inject these 'using' declarations into every cpp file that includes the header?
42083
What are the C++ coding and file organization guidelines you suggest for people who have to deal with lots of interdependent classes spread over several source and header files?  I have this situation in my project and solving class definition related errors crossing over several header files has become quite a headache.
46642
Let's say I have a class that has a member called data which is a list.    I want to be able to initialize the class with, for example, a filename (which contains data to initialize the list) or with an actual list.  What's your technique for doing this?  Do you just check the type by looking at __class__?  Is there some trick I might be missing?  I'm used to C++ where overloading by argument type is easy.