Thread: Structure vs. Class

  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
    28,413
    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).
    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

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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, 11: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, 09:54 AM
  4. Converting a C structure into a C++ class
    By deadpoet in forum C++ Programming
    Replies: 7
    Last Post: 01-07-2004, 02:06 PM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM