Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 495321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:34:56+00:00 2026-05-13T05:34:56+00:00

Studing STL I have written a a simple program to test functors and modifiers.

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T05:34:56+00:00Added an answer on May 13, 2026 at 5:34 am

    The difference you read from other answers is correct. struct is just a class with public accessibility by default. This includes the inheritance modifier. Basically, you should mention public before the base class name when you’re using a class to make those definitions equivalent:

    template <class T>
    class mystruct : public binary_function<T ,T ,bool> {
        public:
        bool operator() (T  i,T  j) const { return i<j; }
    };
    

    Otherwise, the compiler will assume that mystruct is privately inheriting binary_function<T,T,bool>.

    You can verify this fact by changing the struct to:

    struct  mystruct : private binary_function<T ,T ,bool> {
        public: // not required here
        bool operator() (T  i,T  j) const { return i<j; }
    };
    

    which is equivalent to your current definition of the class and see the compiler whine with a similar error message.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 321k
  • Answers 321k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Why not write your own Zend_Auth_Adapter? May 14, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer In answer to your question see here http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm May 14, 2026 at 12:33 am
  • Editorial Team
    Editorial Team added an answer I don't know if mysql can use any of these… May 14, 2026 at 12:33 am

Related Questions

I am currently a CS student at a 4th year(of total 5). I`m studying
I'm studing Microsoft ASP MVC framework. Here is something strange of my page. I
Studying C#, my books are showing me classes for readin files. I've found 2
While studying C++ (and C) I had some particular doubts regarding the working of
Just studying the C style of Jon Bentley's Programming Pearls and was wondering if

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.