struct vs class

This is a discussion on struct vs class within the C++ Programming forums, part of the General Programming Boards category; I am quoting this from a book (C# 3.0 O'reilly.. pg 160 ) "The goal struct is to be lightweight ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    71

    struct vs class

    I am quoting this from a book (C# 3.0 O'reilly.. pg 160 )

    "The goal struct is to be lightweight - requiring little memory overhead ....etc... "

    Is the true for C++ also ?? If yes .. Can any one tell me how is memory saved in structs in comparison to classes ??

    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    It sounds like you are taking the quote out of context. It seems to be with reference to a struct named goal, not to structs in general.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Aren't structs just classes without methods?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Quote Originally Posted by MTK
    Aren't structs just classes without methods?
    In C++, structs are classes whose members and bases have public access by default.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Quote Originally Posted by MTK
    Aren't structs just classes without methods?
    In C++, structs are classes whose members and bases have public access by default.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    So they can have methods? Then why not use a class?

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    There are differences if you ask a C programmer, there are no differences other than default implicit behavior if you ask a C++ programmer.

    Using member functions and stuff (search google) with struct will not work in C, but it will in C++.

    You'd be better off using classes in C++ and structs in C for clarity and such. It might matter depending on what API's you use as well, if there's a requirement for a struct then go with it regardless... but use struct only when you must, in C++.
    Last edited by since; 11-30-2009 at 11:03 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Quote Originally Posted by MTK
    So they can have methods? Then why not use a class?
    Generally people do use the class keyword instead of struct, reserving the struct keyword to denote an aggregate struct by convention.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    c structs are a sort of half solution to the OOP issue. Remember, C was around a long time before C++. C++ being created specifically to add OOP to C. C had some OOP features, but nothing as robust as C++, although using templates, you can fully implement C++ features in C source code. Originally that is how it was done, C++ was merely a set of template files that allowed you to code in C++ and compile it in a C compiler.

    I do not use struct's with methods. If an object needs methods I create a class, if it doesn't, I use a struct. To me, a struct is merely a custom data type, while a class is an actual object.
    Last edited by abachler; 11-30-2009 at 11:03 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Quote Originally Posted by abachler
    C had some OOP features, but nothing as robust as C++, although using templates, you can fully implement C++ features in C source code. Originally that is how it was done, C++ was merely a set of template files that allowed you to code in C++ and compile it in a C compiler.
    I presume you mean "template" in a more general sense, or do you actually mean through the use of a C++ to C compiler/preprocessor?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Grey Wizard C_Sparky's Avatar
    Join Date
    Sep 2009
    Posts
    50
    I just look at it as structures being a class without member functions. At least that's what a book told me.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,981
    Quote Originally Posted by C_Sparky
    I just look at it as structures being a class without member functions. At least that's what a book told me.
    If that book is about C++ and was not suggesting a convention, then that book contains incorrect information.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    Likely the intent was to teach how structures and classes are typically used and thought of, not the exacting semantics of C++.

    But If so, it's still not quite right. Structures are often restricted to POD (Plain Old Data) types, which have special versatility in C++. Other times they are restricted to types that behave like POD types, even if they do not follow the strict POD definition. Other times they are as the book describes.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    C++ was intended to be (roughly) a superset of C. Strictly, this is not true, but it's close enough for this discussion. One of the goals of C++ was that (reasonably written) C code could be compiled with little or no change. This implies two things.

    1. The keyword "struct" cannot be removed from the language
    2. structs must act identically to C, which means they need public access

    Beyond that, there is no reason to LIMIT the functionality of structs in any way that isn't inconsistent with C semantics. In fact, the difference in default visibility is the ONLY difference between structs and classes.

    If backward compatibility with C had not been a goal, structs probably wouldn't exist in C++.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. const elements of struct / class not accessible?
    By talz13 in forum C# Programming
    Replies: 2
    Last Post: 03-24-2006, 04:05 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21