Thread: when to use struct and when to use class?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    17

    when to use struct and when to use class?

    If class and struct are exactly the same except that a struct's members are public by default and a class' members are private by default, what is the difference and how do I decide to use one over the other?

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    use a struct when all you need is to hold data... when you need methods and inheritance and polymorphism and such, use a class.

    Look into OOP concepts.

    unions are fun too.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by jarro_2783
    If class and struct are exactly the same except that a struct's members are public by default and a class' members are private by default, what is the difference and how do I decide to use one over the other?
    Its purely a style issue, but the generally accepted convention is to use struct to mimic functionality of the 'C' struct, ie, only public data members.

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    The only reason the struct keyword is in C++ at all, is for backwards compatibility with C. If you are using C++ with Object Orientied designs, don't use struct at all just use classes.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    IMO, you should never write a class to a binary output file... structs fit the job just perfectly
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by jarro_2783
    If class and struct are exactly the same except that a struct's members are public by default and a class' members are private by default, what is the difference and how do I decide to use one over the other?
    Previous discussion of this elseweb:
    http://forums.devshed.com/c-programm...ct-370713.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    It's entirely style, but I personally:

    * Use structs when I am keeping all the data public. All member variables can be modified directly. Sometimes I have functions (especially constructors) but never anything private.

    * Use classes when I am hiding the data behind a class interface. No direct access to member variables (which are private). The class presents a set of public and protected methods that form the only means of access to the class.

    In general, the struct style I use is useful in some cases when you're passing data of a very specific format, and that format will always be the same. For example, an RGBA color might make a good struct, because I know I need exactly 4 values, 8 bits each. Here, the user of the struct needs to know exactly how the struct's data is stored; changing the way the struct stores data usually means changing the code that uses the struct.

    The class style is better for most other things, especially the more complicated ones. The user of the class doesn't need to know or care about the internal representation of data in the class -- as long as the class adheres to its public and protected interface methods, it doesn't matter how the data is stored in the private variables.

    You could completely recode your entire variable storage mechanism, and you shouldn't need to change a single character of code outside of that class. An example of this might be if you had a class that manages a collection of objects (where you can insert, delete, search, etc objects). Maybe your first iteration of the class uses a std::list for its fast insert and delete, but later you move to a std::map for its rapid search abilities.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In addition to comments above, one instance where I will use structs is when writing a library (and associated header files) that are required to be usable from both C and C++ (i.e. other developers might be using either a C or a C++ compiler). If there is a requirement that the library itself is buildable as either C or C++, then only structs will be used everywhere. If that requirement is relaxed (eg I'm only required to provide a binary form of the library itself, but must provide header files that allow the users of the library a choice of using either a C or C++ compiler) then the header files will only use structs (and no constructors or member functions, as those are not valid C), and the interface of any functions will accept a pointer to struct as argument rather than a reference. The reason for doing this is that it is easier to maintain the header if the data structures and functions are only declared in one manner --- i.e. I avoid having a situation where a header file is required to check if is being compiled with a C or a C++ compiler.

    One other reason for using structs is when using a library that is written in C (eg the POSIX library). I might put a C++ wrapper on that library (i.e. a set of functions that provide a good-style C++ interface, but call C library functions internally), but that wrapper itself will probably use structs for it's internal working, as a lot of non-trivial C libraries work with data structures rather than basic (non-composite or non-struct) types.

    As a general rule, If I'm writing a C++ class library, and don't have to maintain any compatibility with a C compiler, I use classes rather than structs. Therefore the code will choke a C compiler.

    I sometimes use structs is when I'm being lazy answering questions and am giving very elementary code examples to someone who understands the relationship between a struct and a class (i.e. where they are the same and where they are different). It takes less keystrokes to type;
    Code:
    struct X
    {
       /*  some simple stuff */
    }
    rather than
    Code:
    class X
    {
        public:
       /*  some simple stuff */
    }
    if the person I'm giving code to understands that there is no real difference.

Popular pages Recent additions subscribe to a feed