Thread: struct or class?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    struct or class?

    Hello everyone,


    Multiplies is a struct, why MSDN said it is a class?

    http://msdn2.microsoft.com/en-us/lib...ay(VS.80).aspx


    thanks in advance,
    George

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Because a class is a struct, except that it defaults to private access and private inheritance, and a struct is a class, except that it defaults to public access and public inheritance.
    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

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Though it's kinda bad practice to use a struct as a class since most programmers in C++ will not think in similar lines (for lack of a better word). Structs are commonly more thought of as a big data type or a storage for a lot of variables while a class is an object to be created and utilized.
    I don't agree with MSDN on this matter. A struct should be a struct and a class a class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Though it's kinda bad practice to use a struct as a class since most programmers in C++ will not think in similar lines (for lack of a better word). Structs are commonly more thought of as a big data type or a storage for a lot of variables while a class is an object to be created and utilized.
    I declare classes as "struct" all the time. I don't think any experienced C++ programmer is going to be confused.

    Looking at my standard headers, it seems like most of the STL templates are defined as "struct" not "class." I suspect that if you look in your headers you'll see the same.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I declare classes classes and storage struct, and btw, it was matsp who actually mentioned this the first time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    I declare classes classes and storage struct, and btw, it was matsp who actually mentioned this the first time.
    Do whatever you want. I just don't see any stylistic problem with using 'struct.' If it's good enough for the makers of STL it should be good enough for anyone.

    Defaulting to private inheritance is not usually useful. And most data types are going to have at least some public members or they would not be useful either. So, we typically want public inheritance, and public members, so 'struct' fits the bill perfectly. It is a basic fact of C++ that a struct and a class are exactly the same except for some defaults. If people are confused on that point they need to brush up on the fundamentals.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I think the whole public vs. private by default is stupid. Explicitly specify if you want private or public. That's the safest way. A class can do either.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    I think the whole public vs. private by default is stupid. Explicitly specify if you want private or public. That's the safest way. A class can do either.
    So can a struct.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Exactly!
    So it's silly to use a struct just because you want public members by default or a class if you want private members by default.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Exactly!
    So it's silly to use a struct just because you want public members by default or a class if you want private members by default.
    Most useful classes have public AND private members, so you are going to have to type "public" or "private" at least once somewhere. Since I typically declare public members first, I want the default to be public.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I think this is much ado about nothing. The MSDN link defined a function object that does not need any non-public members, especially since it has no state.

    We are looking at the difference between:
    Code:
    template<class Type>
       struct multiplies : public binary_function <Type, Type, Type> 
       {
          Type operator()(
             const Type& _Left, 
             const Type& _Right
          ) const;
       };
    and
    Code:
    template<class Type>
       class multiplies : public binary_function <Type, Type, Type> 
       {
       public:
          Type operator()(
             const Type& _Left, 
             const Type& _Right
          ) const;
       };
    Frankly, no one is going to mistake that function object for a C-style struct if you use the former. For the latter, a problem will occur if the implementor left out the public label, but that's either not going to happen or will be detected at compile time if any tests for this multiples function template were written at all.

    You are free to follow the rule of class for non-POD classes and struct for POD C-style structs, but I do not think it makes a major stylistic difference either way.
    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

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well I just like to keep struct only for PODs since it documents your intent better and most developers (even though they know structs can have functions) usually think of a POD when they see a struct.

    Syntactically there's nothing wrong with using class or struct; it's just a matter of style & preference.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    Frankly, no one is going to mistake that function object for a C-style struct if you use the former.
    I'd go even further and say, nobody should EVER mistake something for a C-style struct, because C-style structs don't exist in C++. Even for something as simple as:

    Code:
    struct foo
    {
        int a, b, c;
    };
    The compiler will generate a default constructor, copy constructor, and assignment operator. A struct in C has none of these things. If this was truly a "C struct" it would NOT have a copy constructor and the following would be illegal:

    Code:
    foo x;
    foo y(x);
    But we know that's perfectly legal. Thus proving that C-style structs DON'T EXIST in C++. It's all the same thing apart from some defaults.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by brewbuck View Post
    Most useful classes have public AND private members, so you are going to have to type "public" or "private" at least once somewhere. Since I typically declare public members first, I want the default to be public.
    Which is a silly argument.
    If I want a public members, I add "public:" to the beginning of the class.
    Using structs/classes depending on the default access is silly IMO.
    Use whatever you want, but don't go around choosing just because of the default visibility. That's what public, protected and private are for.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use whatever you want, but don't go around choosing just because of the default visibility. That's what public, protected and private are for.
    hmm... then what's the default visibility for?
    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. 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