I have a question for operator overloading in structer

Mar 25, 2009 at 2:47am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// for_each example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

struct myclass {
  void operator() (int i) {cout << " " << i;}
} myobject;

int main () {

  myobject(10);
  return 0;
}


I don't understand this code.
myobject(10) is constructor?
Does struct have a constructor like class?

I wonder operator() operation.
struct also have operator overloading in struct area?
Why did it have initializing ()?

Am I sure or not?
I want to know STL, so I study but I get many questions. :)
Last edited on Mar 25, 2009 at 2:55am
Mar 25, 2009 at 12:18pm
1) No, myobject( 10 ) calls operator() with i=10.
A constructor call has the syntax ClassOrStuctName( parameters )
operator() is called via the syntax VariableName( parameters )

2) The only difference between struct and class in Python is default
access in structs is public and in classes is private. Otherwise
they are absolutely identical.

3) Therefore, yes, you can overload operators on structs.
Mar 26, 2009 at 12:36am
Thank you jsmith :D
Your answer is very clear that help me a lot.
I didn't get info like yours until now. :D

Have a nice day!!
Topic archived. No new replies allowed.