Structs or classes?

This is a discussion on Structs or classes? within the C++ Programming forums, part of the General Programming Boards category; Well, the title pretty much explains the question. In all of the programs that I have designed and/or written, I've ...

  1. #1
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156

    Structs or classes?

    Well, the title pretty much explains the question. In all of the programs that I have designed and/or written, I've implemented classes. But when I read my game programming book, the author kindly shows me how some of the structs belonging to the DirectX SDK are implemented, I don't know if I'm missing out on something, but am I the only programmer that uses classes instead of structs? What are the benefits of either?
    MSDN <- Programmers Haven!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    The only difference between a class and a struct is that all members of a class default to private, while all members of a struct default to public. Usage of either 'class' or 'struct' is therefore purely a matter of coding style.

  3. #3
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    In structs, everything is public by default. One author I read said that structs and classes are essentially the same, but if you start making inheritance hierarchies of structs, people will just laugh at you. That's reason enough for me to use classes. I feel its a semantic thing. Classes are for interacting program objects. Structs are for grouping data.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    A main reason to use classes rather than structs is that everything is private by default. From a security and code-integrity perspective, it makes sense that access to attributes, methods, etc be denied unless the programmer deliberately allows greater access. Structs work on the reverse philosophy: access is granted unless explicitly denied. It is easier to get something working right if it is not accessible to anyone or anything who doesn't actually need access to it. Those who can't access something can't break it.

    Security people will describe this as, for example, minimising attack surfaces. Those into engineering design (or configuration control) will describe this as minimising unnecessary dependencies.

    Of course, if you know in advance that every attribute of an object needs to be publicly accessible, then use a struct.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Structs are great for what I would call blocks of data such as file headers, properties of objects, etc. I use them when I have many variables of a class that can be set and get b/c the setters getters become a bit cumbersome. If you are concerned about ownership of the structure you can declare it inside of a class. I've talked to people who never use structs but I say use them when appropriate.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. classes, structs and unions
    By Luciferek in forum C++ Programming
    Replies: 24
    Last Post: 08-09-2008, 10:26 AM
  2. Why use Classes instead of Structs?
    By yaya in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2008, 12:39 AM
  3. Are structs and classes the same thing?
    By dwks in forum C++ Programming
    Replies: 6
    Last Post: 11-25-2005, 02:21 PM
  4. Classes or Structs?
    By DeanDemon in forum C++ Programming
    Replies: 12
    Last Post: 12-05-2002, 02:58 AM
  5. Classes or Structs faster for Lists
    By White Rider in forum C++ Programming
    Replies: 24
    Last Post: 04-05-2002, 02:57 PM

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