CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2009
    Posts
    2,413

    Struct vs Class, take II

    Quote Originally Posted by amira7
    Besides the visibility of members,

    Structs are value types and classes are reference types.

    As you know, value types are allocated on the stack or inline as a part of another object while classes are allocated on the heap.

    Struct is often a small object that needs to be treated like a primitive type while classes are normally used for implementation requiring more processing power. In other words, classes would normally be used to implement a piece of business logic, rather than to support data related or primitive-type objects.

    I hope this clears it up a bit.
    Although your description roughly follows common usage I think it's worth mentioning that the roles you assign to the class and the struct are a historical convention and nothing that's stipulated by the language.

    In fact the struct and the class are conceptually equivalent in C++. Nothing prevents anyone from using them in the exact opposite manner from what you state.

    I mention this because making struct and class to be equals was a very important design decision as explained by Stroustrup in The Design and Evolution of C++ on page 76.

  2. #2
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Struct vs Class, take II

    There is one more major difference between structs and classes. structs can exist inside of an extern "C" block, classes may not.

    Most people use structs to just store data instead of classes, simply because it's faster to write a struct that a class. It doesn't take long to type "public:" but why bother if you don't have to.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Struct vs Class, take II

    Quote Originally Posted by ninja9578
    structs can exist inside of an extern "C" block, classes may not.
    That is not true.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Struct vs Class, take II

    Quote Originally Posted by ninja9578 View Post
    There is one more major difference between structs and classes. structs can exist inside of an extern "C" block, classes may not.
    Code:
    extern "C"
    {
       class x
       {
         int y;
       };
    }
    Code:
    Thank you for testing your code with Comeau C/C++!
    Tell others about http://www.comeaucomputing.com/tryitout ! 
    
    Your Comeau C/C++ test results are as follows: 
    
    
    Comeau C/C++ 4.3.10.1 (Oct  6 2008 11:28:09) for ONLINE_EVALUATION_BETA2
    Copyright 1988-2008 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ C++0x_extensions
    
    
    In strict mode, with -tused, Compile succeeded (but remember, the Comeau online compiler does not link). 
    Compiled with C++0x extensions enabled.
    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Aug 2008
    Posts
    902

    Re: Struct vs Class, take II

    Quote Originally Posted by amira7 View Post
    Besides the visibility of members,

    Structs are value types and classes are reference types.

    As you know, value types are allocated on the stack or inline as a part of another object while classes are allocated on the heap.

    Struct is often a small object that needs to be treated like a primitive type while classes are normally used for implementation requiring more processing power. In other words, classes would normally be used to implement a piece of business logic, rather than to support data related or primitive-type objects.

    I hope this clears it up a bit.
    This is not true. None of it. amira7 is clearly a C# programmer or has somehow gotten confused, reading C# literature.

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Struct vs Class, take II

    Okay, it shouldn't be used in a extern "C" block. Because if you do, it completely breaks all C compatibility.

  7. #7
    Join Date
    May 2009
    Posts
    2,413

    Re: Struct vs Class, take II

    Quote Originally Posted by Chris_F View Post
    This is not true. None of it. amira7 is clearly a C# programmer or has somehow gotten confused, reading C# literature.
    It's true in the sense that it describes common usage fairly well. But, as I noted in my first post, this usage is not stipulated by the C++ language.

    In the book I mentioned (The Design and Evolution of C++, p. 76), Stroustrup explains why he did not keep the C-struct in C++. The reason is that he didn't want one simple and one advanced version of the same concept. He dropped the C-struct and made struct and class equals in order to keep the C++ language together.

    Anyone is free to assign different roles to the struct and the class but that's just a question of convention and personal taste. In C++ "a struct is a class" and according to Stroustrup this notion has "stopped C++ from drifting into becoming a much higher-level language with a disconnected low-level subset".

    So Stroustrup didn't want C++ to become two languages in one and that's why struct and class are now equals. But that doesn't prevent anyone, including me, from treating the struct as if it was the little brother of the class. Even Stroustrup himself subscribes to this usage. (The C++ Programming Language, 3'rd ed., p. 234).
    Last edited by nuzzle; January 11th, 2011 at 03:48 AM.

  8. #8
    Join Date
    Aug 2007
    Posts
    858

    Re: Struct vs Class, take II

    Quote Originally Posted by ninja9578 View Post
    Okay, it shouldn't be used in a extern "C" block. Because if you do, it completely breaks all C compatibility.
    Should only be a problem if you use a ctor/dtor, operator overloading, virtual functions, etc. All of which can be used with either struct or class.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured