operator int()

O

onkar

SomeClass{
public:
int i;
SomeClass(int j):i(j){};
operator int(){
return this->i;
}
// some functions (all of them public) and
constructors ??

};
int main(void){
SomeClass s(10);
int i=s;
cout<<i<<endl;
return 0;
}

I am not able to understand why this is printing 10 ??? How is
"operator int()" function called ??
 
C

Christopher

SomeClass{
public:
int i;
SomeClass(int j):i(j){};
operator int(){
return this->i;
}
// some functions (all of them public) and
constructors ??

};

int main(void){
SomeClass s(10);
int i=s;
cout<<i<<endl;
return 0;

}

I am not able to understand why this is printing 10 ??? How is
"operator int()" function called ??

its not an operator, its a contructor

when you passed 10 in the construction of s, it called the constructor
for SomeClass which had an initializer list and called int's
constructor for data member j passing in an argument of 10. So, J has
a value of 10.
 
R

Rolf Magnus

onkar said:
SomeClass{
public:
int i;
SomeClass(int j):i(j){};
operator int(){
return this->i;
}
// some functions (all of them public) and
constructors ??

};
int main(void){
SomeClass s(10);
int i=s;
cout<<i<<endl;
return 0;
}

I am not able to understand why this is printing 10 ??? How is
"operator int()" function called ??

Well, 'i' is initialized with your object, but 'i' is of type int, and it
needs to be initialized with a value of that type, so 's' must be converted
to int first. And that conversion is done by using your operator int.
 
V

Victor Bazarov

Christopher said:
its not an operator, its a contructor

It's not a constructor. It's called "a type conversion function"
and by virtue of having the word 'operator' in its name, it *is*
an operator.
when you passed 10 in the construction of s, it called the constructor
for SomeClass which had an initializer list and called int's
constructor for data member j passing in an argument of 10. So, J has
a value of 10.

V
 
C

Christopher

It's not a constructor. It's called "a type conversion function"
and by virtue of having the word 'operator' in its name, it *is*
an operator.

I stand corrected.
 
T

thomas

its not an operator, its a contructor

when you passed 10 in the construction of s, it called the constructor
for SomeClass which had an initializer list and called int's
constructor for data member j passing in an argument of 10. So, J has
a value of 10.- Hide quoted text -

- Show quoted text -

How can it be a constructor?
If it is, what's the help of the line
SomeClass(int j):i(j){};

I think it's a converter.
when calling " int i=s; "
it's just like " int i=(int)s; ".
that's where "operator int(){}" works.
 
C

Christopher

How can it be a constructor?
If it is, what's the help of the line
SomeClass(int j):i(j){};

I think it's a converter.
when calling " int i=s; "
it's just like " int i=(int)s; ".
that's where "operator int(){}" works.

I think you fellas are confusing the actual method of the class:
SomeClass operator int();
...which I did not mention at all.

with my discussion of the intialiser list in the constructor of the
class:
SomeClass(int j)
:
i(j)
{};
.... where I am referring to i(j)

Which follows the symantics of a copy constuctor:

int(const int &);
MyType(const MyType &);

Yes, SomeClass::eek:perator int() is a conversion obviously. I thought
the OP was confused how the value 10 was the output and tryed to trace
it for him, from start to finish. I guess I looked at it to quick and
took it for granted that he did not understand the method
SomeClass::eek:perator int and was asking about that, rather than the
initialiser list. My mistake.

See what happens when you don't qualify things lol. I say him ask
about operator int() and took a quick look at hise code and assumed he
was talking about i(j) and had just confused what it was called.
Perhaps the author should have said SomeClass::eek:perator int () and I
should have not read things so quickly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,512
Latest member
FinleyNick

Latest Threads

Top