Studing STL I have written a a simple program to test functors and modifiers. My question is about the difference aon using CLASS or STRUCT to write a functor and try to operate on it with function adaptors.
As far as I understand in C++ the difference beetween CLASS and STRUCT is that in the last case the members are public by default. This is also what I read many times in the answers in this site. So please explain me why this short piece of code will fail to compile even if I declared all members ( just a function overloading () ) public when I try to use the not2 modifier. (I have not tried other modifiers e.g. binders yet)
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
template <class T>
void print (T i) {
cout << " " << i;
}
// In the manual I read:
// "In C++, a structure is the same as a class except that its members are public by default."
// So if I declare all members public it should work....
template <class T>
class mystruct : binary_function<T ,T ,bool> {
public :
bool operator() (T i,T j) const { return i<j; }
};
template <class T>
class generatore
{
public:
generatore (T start = 0, T stp = 1) : current(start), step(stp)
{ }
T operator() () { return current+=step; }
private:
T current;
T step;
};
int main () {
vector<int> first(10);
generate(first.begin(), first.end(), generatore<int>(10,10) );
first.resize(first.size()*2);
generate(first.begin()+first.size()/2, first.end(), generatore<int>(1,17) );
cout << "\nfirst :";
for_each (first.begin(), first.end(), print<int>);
cout << "\nFORWARD SORT :";
sort(first.begin(),first.end(),mystruct<int>()); // OK ! even with CLASS
for_each (first.begin(), first.end(), print<int>);
sort(first.begin(),first.end(),not2(mystruct<int>())); // <--- THIS LINE WILL NOT COMPILE IF I USE CLASS INSTEAD OF STRUCT
cout << "\nBACKWARD SORT :";
for_each (first.begin(), first.end(), print<int>);
cout << endl;
}
Everithing runs as expected if I use:
struct mystruct : binary_function<T ,T ,bool> {
public :
bool operator() (T i,T j) const { return i<j; }
};
Part of the error message I obtain is:
g++ struct.cpp
/usr/include/c++/4.2.1/bits/stl_function.h:
In instantiation of
‘std::binary_negate >’:
struct.cpp:52: instantiated from
here
/usr/include/c++/4.2.1/bits/stl_function.h:116:
error: ‘typedef int
std::binary_function::first_argument_type’ is
inaccessible
/usr/include/c++/4.2.1/bits/stl_function.h:338:
error: within this context
/usr/include/c++/4.2.1/bits/stl_function.h:119:
error: ‘typedef int
std::binary_function::second_argument_type’ is
inaccessible ….
Seems that at least in this case a struct is not equivalent to a class with public members, but why ?
The difference you read from other answers is correct.
structis just aclasswithpublicaccessibility by default. This includes the inheritance modifier. Basically, you should mentionpublicbefore the base class name when you’re using aclassto make those definitions equivalent:Otherwise, the compiler will assume that
mystructis privately inheritingbinary_function<T,T,bool>.You can verify this fact by changing the
structto:which is equivalent to your current definition of the
classand see the compiler whine with a similar error message.