Thread: struct vs class

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    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 12:03 PM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,195
    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 12:03 PM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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, 11:03 PM
  2. const elements of struct / class not accessible?
    By talz13 in forum C# Programming
    Replies: 2
    Last Post: 03-24-2006, 05: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, 07:18 AM