Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Operator overload

Reply
Thread Tools

Operator overload

 
 
semut
Guest
Posts: n/a
 
      11-22-2006
Hi, just like to find out when will this operator overload function
gets triggered?


Code Snippet

class A {


public:
enum enA {
A1, A2, A3
} value;

A();
virtual ~A();
A::enA& operator=(const UIScsiimInitReasonCode::enA& );
operator A::enA() {
return(value);
};


friend ostream& operator<< ( ostream&, const A&);
};

I know of the when will operator << and operator = be called, but I am
not sure when will the
operator A::enA() be called and what is the use (beside calling the
operator function directly like
a->operator A::enA()) ?

thanks

 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      11-22-2006
It is typecast operator. So it is called when instance of A is
typecasted to A::enA (either implictly or explicitly).

Look on following code:

#include <iostream>

class A
{
public:
enum enA { A1, A2, A3 };

A() { }
~A() { }

operator A::enA() { return A2; }
};

void Fn(A::enA e)
{
std::cout << "Fn(A::enA)\n";
}

void Fn(...)
{
std::cout << "Fn(...)\n";
}

int main()
{
A a;
Fn(a); // Will call Fn(A::enA) due to overload of operator A::enA()
return 0;
}

There are 2 overloaded functions Fn. You can guess, which one is
called, when parameter is instance of A.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to handle the lack of operator overload ? Mr Smith Java 7 02-10-2005 08:37 AM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM
Q: Overload operator new/delete Jakob Bieling C++ 0 08-08-2003 12:20 PM
overload operator - Matthew Monopole C++ 1 08-08-2003 09:39 AM



Advertisments