473,765 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading normal functionality ???

Hi all,
I just wanted to know if it is possible to overload the
original meaning of operators. I read that it is not possible to
change the operator precedence by using operator overloading. Other
than that i think that the operator functionality can be overloaded.
But in the following code, the operator + has been overloaded on type
int. But it does'nt seem to work as the operator fn was not invoked.

In the following code,

# include <iostream>
using namespace std;

namespace blackbox
{
class A
{
public:
int a;
int operator *(int a);
};

int A::operator * (int b)
{
return a+b;
}
}

int main()
{
using namespace blackbox;

int a=10;
int b=20;
int c=a*b;
cout << c << endl;

return 0;
}

OUTPUT:
---------------
200
Thanks and regards,
Sarathy

Aug 9 '06 #1
10 1409

sarathy 写道:
Hi all,
I just wanted to know if it is possible to overload the
original meaning of operators. I read that it is not possible to
change the operator precedence by using operator overloading. Other
than that i think that the operator functionality can be overloaded.
But in the following code, the operator + has been overloaded on type
int. But it does'nt seem to work as the operator fn was not invoked.

In the following code,

# include <iostream>
using namespace std;

namespace blackbox
{
class A
{
public:
int a;
int operator *(int a);
};

int A::operator * (int b)
{
return a+b;
}
The operator you overloaded has a two parameters, one of the type A and
the other of type int. To overload the operator * for two ints, you
should use int operator * (const int &, const int &) instead.
>

int main()
{
using namespace blackbox;

int a=10;
int b=20;
int c=a*b;
cout << c << endl;

return 0;
}

OUTPUT:
---------------
200
Thanks and regards,
Sarathy
Aug 9 '06 #2
sarathy wrote:
Hi all,
I just wanted to know if it is possible to overload the
original meaning of operators. I read that it is not possible to
change the operator precedence by using operator overloading. Other
than that i think that the operator functionality can be overloaded.
But in the following code, the operator + has been overloaded on type
int. But it does'nt seem to work as the operator fn was not invoked.


>
In the following code,

# include <iostream>
using namespace std;

namespace blackbox
{
class A
{
public:
int a;
int operator *(int a);
};

int A::operator * (int b)
{
return a+b;
}
}

int main()
{
using namespace blackbox;

int a=10;
int b=20;
int c=a*b;
cout << c << endl;

return 0;
}

OUTPUT:
---------------
200
I'm not sure I fully understand what you're after. :-).
# include <iostream>

namespace blackbox
{
class A
{
int a;
public:
A():a(0) {}
A(int val): a(val) {}
friend int operator * (const A& lhs, const A& rhs);

};

int operator * (const A& lhs, const A& rhs)
{
return lhs.a + rhs.a;
}
}
int main()
{
using namespace blackbox;

A a = 10;
A b = 20;

int c = a * b;

std::cout << c << std::endl;

return 0;
}

Regards,
Sumit.
Aug 9 '06 #3
#include <iostream>

namespace blackbox
{
class A
{
public:
A(int i): a(i) {}
int a;
int operator *(int a);
};
int A::operator * (int b)
{
return a+b;
}
}
int main()
{
using namespace blackbox;

A a(10);
int b=20;

//This is the operator you overloaded, one of type A, the other of
type int.
int c=a*b;
cout << c << endl;

return 0;
}

Aug 9 '06 #4
"sarathy" <sp*********@gm ail.comwrote in message
news:11******** ************@h4 8g2000cwc.googl egroups.com
Hi all,
I just wanted to know if it is possible to overload the
original meaning of operators. I read that it is not possible to
change the operator precedence by using operator overloading. Other
than that i think that the operator functionality can be overloaded.
But in the following code, the operator + has been overloaded on type
int. But it does'nt seem to work as the operator fn was not invoked.

In the following code,

# include <iostream>
using namespace std;

namespace blackbox
{
class A
{
public:
int a;
int operator *(int a);
};

int A::operator * (int b)
{
return a+b;
}
}

int main()
{
using namespace blackbox;

int a=10;
int b=20;
int c=a*b;
cout << c << endl;

return 0;
}

OUTPUT:
---------------
200

You have defined operator* as a member variable of A, yet you do not have an
A object as your left-hand operand, so this could not possibly work.

Give A a constructor:

A(int x) : a(x)
{}

and then try:

A a(10);
int c = a*20;

Note that you cannot overload operators to work solely on built-in types
like ints. You can, however, overload binary operators to operate on a
mixture of user defined and built-in types. These don't have to be member
functions (and cannot be if the int is the left hand operand), e.g.,

struct S
{};

// non-member operator
int operator*(int lhs, const S&rhs)
{
return lhs+5;
}
int main()
{
S s;
int x = 8*s;
cout << x << endl;
return 0;
}

Incidentally, stop naming everything a or b. You are only confusing
yourself.
--
John Carson
Aug 9 '06 #5
Hi,
Note that you cannot overload operators to work solely on built-in types
like ints.
With member Operator Functions [ OF ] i understand it is not
possible to overload only built in type operands, because the member OF
requires an object to invoke it.
But this is not the case with a global and friend operator
functions. They dont need an object to operate upon. They just operate
on the args provided. I am sure that operator functions can be made
global, so that other classes can utilize them.

Hence an operator functions such as

Files operator + (Files f1, Files f2)
{
Files f = f1.getCount() + f2.getCount();
return f;
}

can surely exist as a global/friend OF and will be invoked by a line
as

Files f1(10), f2(20, f3;
f3 = f1+f2;

If the compiler can match the line f1+f2 to the global operator
function above correctly, then why is it not possible for a similar
match involvng only primitives ?

Is it a restriction in the compiler or am i going wrong anywhere ??

2. What does the following constructor do ??

A(int x, int y, int z):a(x),b(y),c( z) {}

I probably guess that it initializes its member variables. But i
remember that such a syntax is being used by a subclass constructor to
pass args to a superclass constructor. If so what happens if class A
had a superclass with names a, b and c AND class A with data members
a,b and c.

What will be the result ??

Regards,
Sarathy

Aug 9 '06 #6
sarathy wrote:
Hi,
>Note that you cannot overload operators to work solely on built-in types
like ints.

With member Operator Functions [ OF ] i understand it is not
possible to overload only built in type operands, because the member OF
requires an object to invoke it.
But this is not the case with a global and friend operator
functions. They dont need an object to operate upon. They just operate
on the args provided. I am sure that operator functions can be made
global, so that other classes can utilize them.

Hence an operator functions such as

Files operator + (Files f1, Files f2)
{
Files f = f1.getCount() + f2.getCount();
return f;
}

can surely exist as a global/friend OF and will be invoked by a line
as

Files f1(10), f2(20, f3;
f3 = f1+f2;

If the compiler can match the line f1+f2 to the global operator
function above correctly, then why is it not possible for a similar
match involvng only primitives ?
Because the standard says so: built-in operators take only operands with
non-class type, and operator overload resolution occurs only when an
operand expression originally has class or enumeration type, operator
overload resolution can resolve to a built-in operator only when an operand
has a class type that has a user-defined conversion to a non-class type
appropriate for the operator, or when an operand has an enumeration type
that can be converted to a type appropriate for the operator. [from 13.6/1]

Is it a restriction in the compiler or am i going wrong anywhere ??

2. What does the following constructor do ??

A(int x, int y, int z):a(x),b(y),c( z) {}

I probably guess that it initializes its member variables. But i
remember that such a syntax is being used by a subclass constructor to
pass args to a superclass constructor. If so what happens if class A
had a superclass with names a, b and c AND class A with data members
a,b and c.

What will be the result ??
Did you try?

Best

Kai-Uwe Bux
Aug 9 '06 #7
Hi,
>
Because the standard says so: built-in operators take only operands with
non-class type, and operator overload resolution occurs only when an
operand expression originally has class or enumeration type, operator
overload resolution can resolve to a built-in operator only when an operand
has a class type that has a user-defined conversion to a non-class type
appropriate for the operator, or when an operand has an enumeration type
that can be converted to a type appropriate for the operator. [from 13.6/1]
Can i take this stmt this way.

1. When the operands to a builtin operator are primitives [non-class
type], the operator's orignial meaning will be used.

2. When the operands to a builting operator are class type or
enumeration, then the compiler will search for the operator functions
corresponding to the class/enum. Hence the overloaded meaning of the
operator will be used.
What will be the result ??

Did you try?
Yes. The thing is that it was initializing the derived class data
members first, before going for the base class constructor
initialization. But when the derived class member variables were
removed the base constructors are initialized correctly.

Can you explain the reason for this behaviour?

Regards,
Sarathy

Aug 9 '06 #8
"sarathy" <sp*********@gm ail.comwrote in message
news:11******** **************@ i42g2000cwa.goo glegroups.com
>
2. What does the following constructor do ??

A(int x, int y, int z):a(x),b(y),c( z) {}

I probably guess that it initializes its member variables. But i
remember that such a syntax is being used by a subclass constructor to
pass args to a superclass constructor. If so what happens if class A
had a superclass with names a, b and c AND class A with data members
a,b and c.

What will be the result ??
Sensible people don't write classes like that.

I believe the answer is that the member a in the derived class will hide the
name of the base class, so you won't be able to call the base class
constructor (the default constructor, if any, will be called for the base
class). You could work around this by using the fully qualified name of the
base class, e.g., ::a, if the base class is in the global namespace:

A(int x, int y, int z):a(x),b(y),c( z), ::a(y) {}
--
John Carson
Aug 9 '06 #9
sarathy wrote:
Hi,
>>
Because the standard says so: built-in operators take only operands with
non-class type, and operator overload resolution occurs only when an
operand expression originally has class or enumeration type, operator
overload resolution can resolve to a built-in operator only when an
operand has a class type that has a user-defined conversion to a
non-class type appropriate for the operator, or when an operand has an
enumeration type that can be converted to a type appropriate for the
operator. [from 13.6/1]

Can i take this stmt this way.

1. When the operands to a builtin operator are primitives [non-class
type], the operator's orignial meaning will be used.

2. When the operands to a builting operator are class type or
enumeration, then the compiler will search for the operator functions
corresponding to the class/enum. Hence the overloaded meaning of the
operator will be used.
It's a little more complicated: overload resolution tries to find what is
considered a "best match". Now, the precise rules are in the standard, and
I just stay away from writing code that requires me to know the details.
Fortunately, that is easy.
What will be the result ??

Did you try?

Yes. The thing is that it was initializing the derived class data
members first, before going for the base class constructor
initialization. But when the derived class member variables were
removed the base constructors are initialized correctly.

Can you explain the reason for this behaviour?
Not without seeing the code. Post a minimal, complete example that
illustrates your observation.
Best

Kai-Uwe Bux
Aug 9 '06 #10

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

Similar topics

3
9411
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am presenting below a summary of what I have gathered. I would appreciate if someone could point out to something that is specific to Borland C++ and is not supported by the ANSI standard. I am also concerned that some of the information may be outdated...
5
5246
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that discussions of new and delete is a pretty damn involved process but I'll try to stick to the main information I'm looking for currently. I've searched around for about the last too weeks and have read up on new and overloading it to some extent but...
45
3291
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
10
3381
by: Mihai Osian | last post by:
Hi everyone, Given the code below, can anyone tell me: a) Is this normal behaviour ? b) If it is, what is the reason behind it ? I would expect the A::method(int) to be inherited by B. Compiler: gcc 4.1, Linux Thanks,
15
2785
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
3
2041
by: Chameleon | last post by:
What is better if you want upcasting in intermediate classes like below? Multiple Inheritance and Overloading or simply RTTI? RTTI wants time but MI and Overloading create big objects (because of virtual) and finally they need time too to access objects inside big object. (Examples looks complicated but are very simple) Paradigm #1: MI & Overloading =============================
12
2348
by: y-man | last post by:
Hi, I am creating a child class of the vector, which contains a couple of functions to make the work with carthesian coordinate vectors easier. To make things work more easily, I would like to be able to access the vector through a string which is either x, y or z. So that I can use: ---xyzvec.hh-- #ifndef XYZVEC_HH #define XYZVEC_HH
2
3201
by: Bharath | last post by:
Hello All, Can you please let me know if we can do pointer arthrmetic using operator overloading? If not, can you please explain why it's not supported by compiler? I tried below e.g. which was giving me error. typedef class x { }X;
14
562
by: Pramod | last post by:
I have one question. Can I catch exception in function overloading.In my programm i want to create two function with same signature and same return type. I know its not possible...can i use try.catch .block to hand this exception.
0
10153
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
10007
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
9946
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
9832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7371
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
5272
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3921
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
3530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.