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
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
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
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.
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.
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.
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.
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:
andCode:template<class Type> struct multiplies : public binary_function <Type, Type, Type> { 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.Code:template<class Type> class multiplies : public binary_function <Type, Type, Type> { public: Type operator()( const Type& _Left, const Type& _Right ) const; };
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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:
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:struct foo { int a, b, c; };
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.Code:foo x; foo y(x);