Structure vs. Class

This is a discussion on Structure vs. Class within the C++ Programming forums, part of the General Programming Boards category; What is the different between the two? Just visibility issues? So you can think of a class as an advanced ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    141

    Structure vs. Class

    What is the different between the two? Just visibility issues? So you can think of a class as an advanced structure

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Essentially, struct and class are interchangable if we ignore visibility. Using visibility keywords allows us to "change one to the other":

    Code:
    struct x {
    private:
    ...
    };
    is the same as
    Code:
    class x
    {
    ...
    };
    And these two are the same:
    Code:
    class y
    {
    public:
      ... 
    };
    
    struct y
    {
    ...
    };
    I personally prefer to use struct to indicate that the object is "plain data" and class to indicate objects that are not "plain data". "plain data" here means that there are no (explicit) constructors and no other member functions. That is a personal/company style choice, however, and others may have other opinions/thoughts on the subject.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,328
    Also along the lines of visibility, note that classes declared with the class keyword have private base classes by default (i.e., private inheritance is the default), while those declared with the struct keyword have public base classes by default (i.e., public inheritance is the default).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> I personally prefer to use struct to indicate that the object is "plain data".
    It's not just you, this is a common idiom.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Daved View Post
    >> I personally prefer to use struct to indicate that the object is "plain data".
    It's not just you, this is a common idiom.
    Good to know that I'm not alone!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help to build network class
    By weeb0 in forum C++ Programming
    Replies: 0
    Last Post: 02-01-2006, 10:33 AM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 08:54 AM
  4. Converting a C structure into a C++ class
    By deadpoet in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2004, 01:06 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM

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