473,665 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Operator overload

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 UIScsiimInitRea sonCode::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

Nov 22 '06 #1
1 1601
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.

Nov 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2064
by: bsaucer | last post by:
I am creating a class with operator overloads. It makes references to another class I've created, but do not wish to modify. I try to overload an operator having arguments having the other class type, but none of the new class type. However it does return a type of the new class. The compiler tells me that at least one parameter must be of the containing type. Is there a way to do this? The other class hos no reference to this new class. Is...
5
3796
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more complex operator, I get stuck. Here's a brief description what I want to do. I want to simulate a matrix (2D array) from a 1D array. so what I have so far is something like this: class Matrix
4
1891
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement as a char rather than incorrectly representing it as an int. I've done this because I want to develop the class so that it will be able to convert between values, ie if I add 500 m to 1 km I'd like a correct result given in metres (1500 m in this...
7
4666
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++ copyconstructor1.cpp #copyconstructor1.cpp: In function `int main()': #copyconstructor1.cpp:86: no match for `sample& = sample' operator #copyconstructor1.cpp:53: candidates are: sample sample::operator=(sample&) ...
17
2504
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: public static bool operator !=(MyType x, MyType y) { return !(x == y); } That way the == operator handles everything, and extra comparing logic isn't
7
1574
by: Genival Carvalho | last post by:
Hello friends... Inside my class how do to use explicit using Overloaded operator or use default ? Ex: Dim x as Integer = A + B ' Use default + operator Dim OV as integer = X + Z ' I will need use Overloaded + Operator ??? Thanks and sorry my bad English.
9
2377
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I attempt to check to see if the object is null: "The call is ambiguous between the following methods or properties: 'TestObject.operator ==(TestObject, string)' and 'TestObject.operator ==(TestObject, TestObject)" Does anyone have any idea how to...
5
2290
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason my C++.NET 2.0 textbook does not have one. I tried to build one using the format as taught in my regular C++ book, but I keep getting compiler errors. Some errors claim (contrary to my book) that you cannot use a static function, that you must...
3
3273
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj, obj2 ;
11
4137
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my mind) and it would be fairly generic. The only sidecase I can see is that operator. itself would not be looked up through operator. . I read that there was previous debate on the subject, but I haven't been able to find why it was rejected.
0
8863
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8779
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7376
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4356
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2765
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2004
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1761
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.