I was working with GTK in one of my projects and I noticed that the library supports inheritance. As you can type-cast a child struct to its parent struct and vice versa. Other than GTK I've never seen this used (so flawlessly):  struct parent p = {5}; struct child c; c = (struct child)p; c.b = 1; //c.a = 5, c.b = 1   Does using the parent struct as the first element do this? As this would seem much more neat. But could padding and aligning interfere?  struct parent { int a; } struct child  { struct parent p; int b; }    Or does rewriting all parent data make it happen?   struct parent { int a; } struct child  { int a; int b; }   EDIT I am NOT looking to implement all pillars of OOP in C. My question is only involving type-casting the data in parent and child structs. I have found that GTK uses GObject to do this, but what must be done to achieve what GObject is doing as far as type casting?
120057
What would be a set of nifty preprocessor hacks (ANSI C89/ISO C90 compatible) which enable some kind of ugly (but usable) object-orientation in C?  I am familiar with a few different object-oriented languages, so please don't respond with answers like "Learn C++!". I have read "" (beware: PDF format) and several other interesting solutions, but I'm mostly interested in yours :-)!    See also
111702
Is it possible to assign a base class object to a derived class reference with an explicit typecast in C#?.  I have tried it and it creates a run-time error.
12125
This question was .  Now I'd like to learn the differences between a struct and a class in C++. Please discuss the technical differences as well as reasons for choosing one or the other in OO design.  I'll start with an obvious difference:   If you don't specify public: or private:, members of a struct are public by default; members of a class are private by default.   I'm sure there are other differences to be found in the obscure corners of the C++ specification.
40421
What are the best practices for modeling inheritance in databases?  What are the trade-offs (e.g. queriability)?  (I'm most interested in SQL Server and .NET, but I also want to understand how other platforms address this issue.)
36061
Is it completely against the Java way to create struct like objects?  class SomeData1 {     public int x;     public int y; }   I can see a class with accessors and mutators being more Java like.  class SomeData2 {     int getX();     void setX(int x);      int getY();     void setY(int y);      private int x;     private int y; }   The class from the first example is notationally convenient.  // a function in a class public int f(SomeData1 d) {     return (3 * d.x) / d.y; }   This is not as convenient.  // a function in a class public int f(SomeData2 d) {     return (3 * d.getX()) / d.getY(); }
30943
I think there must be something subtle going on here that I don't know about. Consider the following:  public class Foo&lt;T&gt; {   private T[] a = (T[]) new Object[5];    public Foo() {     // Add some elements to a   }    public T[] getA() {     return a;   } }   Suppose that your main method contains the following:  Foo&lt;Double&gt; f = new Foo&lt;Double&gt;(); Double[] d = f.getA();   You will get a CastClassException with the message java.lang.Object cannot be cast to java.lang.Double.  Can anyone tell me why? My understanding of ClassCastException is that it is thrown when you try to cast an object to a type that cannot be casted. That is, to a subclass of which it is not an instance (to quote the documentation). e.g.:  Object o = new Double(3.); Double d = (Double) o; // Working cast String s = (String) o; // ClassCastException   And it seems I can do this. If a was just a T instead of an array T[], we can get a and cast it without a problem. Why do arrays break this?  Thanks.
107586
Can a class overload methods that also exist in the publicly inherited interface? It seems like this is unambiguous and useful, but compilers (VC, Intel, GCC) all complain, at least by my construction. Below is a toy example. The inherited rebound() function has two clear overloads, yet this will not compile.  If you rename the rebound() method in either class, it works fine, but if they share the same member function name (even though they're overloaded with different argument types!) you get a fatal error of "too few arguments to function call."  The workaround is trivial (I'll just rename the methods) but I'm just trying to understand if this is a C++ restriction (and why it would be).   #include  class Bound { public:   Bound() : x0(0.0), x1(0.0) {};   Bound(double x) : x0(x), x1(x) {};   double width() const {return x1-x0;}   void rebound(const Bound *a, const Bound *b); private:   double x0, x1; };  void Bound::rebound(const Bound *a, const Bound *b) {   if (a && b) {     x0=std::min(a->x0, b->x0);     x1=std::max(a->x1, b->x1);   } }  class Node : public Bound { public:   Node(double x) : Bound(x), left(0), right(0) {};   Node(Node *a, Node *b) : left(a), right(b) {rebound();}   void rebound() { rebound(left, right); } private:   Node *left;   Node *right; };   int main() {   Node A(1.0);   Node B(2.0);   Node C(&A, &B); }
16535
Is there any good example to give the difference between a struct and a union? Basically I know that struct uses all the memory of its member and union uses the largest members memory space. Is there any other OS level difference?
15076
I have code like this:  class RetInterface {...}  class Ret1: public RetInterface {...}  class AInterface {   public:      virtual boost::shared_ptr&lt;RetInterface&gt; get_r() const = 0;      ... };  class A1: public AInterface {   public:      boost::shared_ptr&lt;Ret1&gt; get_r() const {...}      ... };   This code does not compile.  In visual studio it raises     C2555: overriding virtual function return type differs and is not   covariant   If I do not use boost::shared_ptr but return raw pointers, the code compiles (I understand this is due to  in C++). I can see the problem is because boost::shared_ptr of Ret1 is not derived from boost::shared_ptr of RetInterface. But I want to return boost::shared_ptr of Ret1 for use in other classes, else I must cast the returned value after the return.      Am I doing something wrong?  If not, why is the language like this - it should be extensible to handle conversion between smart pointers in this scenario? Is there a desirable workaround?
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; }
47950
Why don't more mainstream statically typed languages support function/method overloading by return type?  I can't think of any that do.  It seems no less useful or reasonable than supporting overload by parameter type.  How come it's so much less popular?
97115
Is it appropriate to access a class' private members by casting it to a void pointer and then to a struct?    I don't think I have permissions to modify the class that contains the data members that I need to access.  I don't want to take a risk accessing the data members in an indirect way if it is not ethical.  EDIT: Had to edit this further... I am pretty sure the class wouldn't be modified, so it's ok to that extent... my only concern is, if the person who coded that class gets to know of this, it might not go down well with him :(.
113530
I want to make this specialized w/o changing main. Is it possible to specialize something based on its base class? I hope so.  -edit-  I'll have several classes that inherit from SomeTag. I don't want to write the same specialization for each of them.  class SomeTag {}; class InheritSomeTag : public SomeTag {};  template &lt;class T, class Tag=T&gt; struct MyClass { };  template &lt;class T&gt; struct MyClass&lt;T, SomeTag&gt; {     typedef int isSpecialized; };  int main() {     MyClass&lt;SomeTag&gt;::isSpecialized test1; //ok     MyClass&lt;InheritSomeTag&gt;::isSpecialized test2; //how do i make this specialized w/o changing main()     return 0; }
13218
Please help us settle the controversy of "Nearly" everything is an object (). I thought that was the case as everything in Visual Studio at least appears as a struct. Please post a reference, so that it doesn't become "modern jackass" ().  Note that this question refers to C#, not necessarily .NET, and how it handles the data under the hood (obviously it's all 1's and 0's).  Here are the comments to "everything is an object":   Eh, no, it's not. – Binary Worrier I'd like an example... – scotty2012 isn't everything derived from the base type Object? – rizzle Most things are objects...  – Omar Kooheji Value types, ints, doubles, object references (not the objects them selves) etc aren't objects. They can be "boxed" to look like objects (e.g. i.ToString()) but really they're primitive types. Change the entry to "NEARLY everthing is an object" and I'll remove the downvote – Binary Worrier I appreciate the clarification. I think the lowest level that you can interact with, say an int, in C# is as a struct, which isn't an object? -  – rizzle Doesn't Int32 inherit from ValueType which inherits from Object? If so, despite the behavior, an int is an object. – Chris Farmer No, the boxed type for int inherits from ValueType, which inherits from Object. They're not objects in the traditional sense because a) an int isn't a reference to an int, IT IS the int. b) ints aren't garbage collected. If you declare an Int32, then that int is 4 bytes on the stack, end of story – Binary Worrier   Definition of object: "Object" as a inheritor of class System.Object vs. "object" as an instance of a type vs. "object" as a reference type."
18083
I have seen many programs consisting of structures like the one below  typedef struct  {     int i;     char k; } elem;  elem user;   Why is it needed so often? Any specific reason or applicable area?
2880
Why prefer composition over inheritance?  What trade-offs are there for each approach?  When should you choose inheritance over composition?
60675
As a general rule, I prefer using value rather than pointer semantics in C++ (ie using vector&lt;Class&gt; instead of vector&lt;Class*&gt;). Usually the slight loss in performance is more than made up for by not having to remember to delete dynamically allocated objects.  Unfortunately, value collections don't work when you want to store a variety of object types that all derive from a common base. See the example below.  #include &lt;iostream&gt;  using namespace std;  class Parent {     public:         Parent() : parent_mem(1) {}         virtual void write() { cout &lt;&lt; "Parent: " &lt;&lt; parent_mem &lt;&lt; endl; }         int parent_mem; };  class Child : public Parent {     public:         Child() : child_mem(2) { parent_mem = 2; }         void write() { cout &lt;&lt; "Child: " &lt;&lt; parent_mem &lt;&lt; ", " &lt;&lt; child_mem &lt;&lt; endl; }          int child_mem; };  int main(int, char**) {     // I can have a polymorphic container with pointer semantics     vector&lt;Parent*&gt; pointerVec;      pointerVec.push_back(new Parent());     pointerVec.push_back(new Child());      pointerVec[0]-&gt;write();      pointerVec[1]-&gt;write();       // Output:     //     // Parent: 1     // Child: 2, 2      // But I can't do it with value semantics      vector&lt;Parent&gt; valueVec;      valueVec.push_back(Parent());     valueVec.push_back(Child());    // gets turned into a Parent object :(      valueVec[0].write();         valueVec[1].write();          // Output:     //      // Parent: 1     // Parent: 2  }   My question is: Can I have have my cake (value semantics) and eat it too (polymorphic containers)? Or do I have to use pointers?