Thread: Struct vs. Classes?

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Struct vs. Classes?

    What exactly is the difference between a struct and a class?
    What are the advantages/disadvantages of one over the other?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    One has private members, one has public members. Read your book.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    I am reading my book, and I found a code example with a struct with both private and public members...
    And I already know a class can have private, protected, and public members, so what is the difference? Can a struct not have protected members, or derived structs?

    Why would one use a struct instead of a class? Do they allocate different sizes of memory, maybe the class more than the struct?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You book should have already explained the difference. They are identical except for the default scope.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Right. I think it did say something about that, but I forgot it until you mentioned it.
    (Btw...do you know what book I'm reading?)

    So...the default scope for a class is public, and the default scope for a struct is private?

  6. #6
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by Programmer_P View Post
    So...the default scope for a class is public, and the default scope for a struct is private?
    Opposite.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  7. #7
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Ahh, ok, thanks.

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Structs cannot have methods other than a constructor I believe....
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  9. #9
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by jeffcobb View Post
    Structs cannot have methods other than a constructor I believe....
    Um, no?
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by jeffcobb View Post
    Structs cannot have methods other than a constructor I believe....
    Fail!
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by cpjust View Post
    Fail!
    Sorry, that is all I have ever seen in a struct....and it was just initializing members. I have seen structs with function pointers before but that was a syntactic mess.....so is your FAIL that they cannot have ctors or that they cannot have anything or they can have everything?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  12. #12
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    The ONLY difference is the default scope.

    A struct can have anything a class can have.

  13. #13
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I'll be, y'all are right. I have not seen structs used in such a fashion but GCC has no problem with it. Learn something new everyday...

    ^__^
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    To elaborate: a struct is a class for which bases and members have public access by default; a class is a struct for which bases and members have private access by default. Often a convention is adopted such that the struct keyword is reserved for defining POD structs or a simple aggregation of objects.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  4. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM