Thread: Class or Struct?

  1. #1

    Question Class or Struct?

    I have been wondering which is better to use? A struct or a class? I have been thinking it would be a class since it's members are private by default. But I have no clue...

  2. #2
    Join Date
    Aug 2001
    Groningen (NL)
    I don't think there is such as better here, but using a class is more common. From that point of view it might by handier to use a class, especially when using a library which uses classes it leads to more consistency and easier design. And it might be easier to read, a lot of people associate a structure with the struct as used in C while they associate class with the C++ class.

  3. #3
    Registered User
    Join Date
    Nov 2002
    I don't think there is a better here as well. I agree with Shiro. But structs in C++ are most commonly used with data members while classes more commonly include the member functions.

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    First of all, Do you know the difference between them?
    If Yes... Then use what you want
    If No... Then this thread should be about the difference between them.

  5. #5
    I thought the only difference was that a structs members are public by default while a classes are private by default. And that a class was made from structures and have expanded functionality. Is this what the difference is?

  6. #6
    A class isn't made from a struct; a class is a struct with data members set to private by default. That's it. However, it is good coding practice to label the access of classes anyway. Personally, I use structs exclusively in the C form, so private and protected data members are ruled out.

    I have seen structs used #define'd as interface in COM coding in C++, but this is an exception to the rule.

  7. #7
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001


    Use whichever you want... it's not a large dilemma =P

    Personally I prefer class for things with functions, constructors, etc. and I use struct for things that are just data. Although you can put just data in a class and all the virtual functions and assignment operators you want in a struct =)

  8. #8
    I say use class, unless you only have public data members, use a struct.

    I see so much c++ code using c-ish structs, that I freak out everytime I see the word struct in a c++ program- but that's just me

    Anyways, doesn't matter.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Göteborg, Sweden
    There's really no big difference between a class and a struct, apart from struct having public members as default.
    Structs can have all features classes can, like constructors and inheritance. But most of the time structs are used as simple data-containers.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User
    Join Date
    Dec 2002

    An added information

    When you apply inheritance to structs they are inherited publicly by default
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  11. #11
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    I use structs simply to store like data like time (mins, hours, secs) or address (house number, line1, line2, post/zip code) but I would use a class when I wanted to also manipulate that data, have the data members private and the access functions public like

        int hours;
        int mins;
        int secs;
        int gethours(){return hours;};
    Compiler == Visual C++ 6.0
    "Come Out Fighting."

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. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM