473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

What are implicit/explicit overloading operators

I have this book on C++ and evidently it seems to be going above my head when it comes to overloading. I have a few questions...

1) what is overloading and why would you use it?

2) What are some examples of implicit/Explicit overloading?

3) Why would I want to use Explicit VS. Implicit overloading?

PLEASE HELP...

Thanks

Dragon
Oct 31 '06 #1
5 7449
Banfa
9,065 Expert Mod 8TB
1) what is overloading and why would you use it?
Where you have the same function but it takes different parameters. Normally you use it where you want to perform the same operation from different parameter types, for instance operator+ in the string class operates on strings, char * and char (I think).

2) What are some examples of implicit/Explicit overloading?
Implicit overloading happens if you cdon't create any Constructors for a class, the compiler implicitly creates a default and a copy Constructor. Explicit overloading happens any place you explicitly write 2 functions with the same name and different set's of parameters.

3) Why would I want to use Explicit VS. Implicit overloading?
If you class has members that point to allocated memory then the implicit copy constructure can cause memory problems so you would explicitly overload it youself to prevent such errors.
Oct 31 '06 #2
Thanks for the information..
I do have some additonal questions as I trying to read and understand overloading.

I read one part where it says there is no IMPLICIT overloading of operators but in the back of the chapter it has a question about giving examples of implicit operator overloading???

So is impliict overloading allowed in C++ or not, if so what are some examples of implicit overloading and it what situation is EXPLICIT overloading preferred over IMPLICIT overloading??

I am so confused

Dragon
Oct 31 '06 #3
vinodV
4
I don't really kno abt implicit/explicit overloading, but the way the other person explained it sounds fine 2 me. so going by his explanation, say u have a class and u create dynamic stuff in the class. in c++ u don't have a garbage collector or anything provided with the compiler like the 1 in java which would get rid of the dynamic stuff u create. so u'll need a destructor in c++. if u don't have 1 the compiler would provide a default 1. and everytime an object goes out of scope the destructor is invoked. the 1 provided by the compiler would go & delete the static stuff but not the dynamic stuff. so if u do create dynamic stuff this is gonna be a problem. in this situation u would wanna create a destructor of ur own which would go & delete the dynamic stuff u created, failing which u would have something called memory leak. so i guess this would be a good reason 2 use explicit overloading. so although the compiler provides a lot of default stuff people would suggest 2 create ur explicit stuff which i guess would be explicit overloading 2 be on the safe side. constructors 4 example. so if u don't really kno wat's happening just do stuff explicitly. so coming back 2 the example the other person had copy constructors. if u use the default 1 the compiler provides it would just go ahead & copy all the data members. all problems arise wen u have dynamic stuff. so now u have 2 objects pointed 2 the same dynamic stuff. now if delete some of the dynamic stuff in 1 of the objects the other object wouldn't kno that. so u have memory leak rite there. this other object would be pointing 2 stuff that don't even exist.

& abt ur other question, i did do operator overloading but i don't think i have the answer 2 that question.
Feb 12 '07 #4
vinodV
4
I guess the example 4 implicit operator overloading would be say u have an operator. u kno how u could use the operator on operands of different types. maybe that's an example of implicit operator overloading. u kno how u could use some operators on operands each of which r of a different kind. for example <, u could use it with int & float.
Feb 12 '07 #5
RRick
463 Expert 256MB
There's also something called implicit/explicit conversions for parameters to methods/operators. In this case the compiler will convert a parameter to something else using constructors or implicit converting rules for primitives like ints.

For example, say you have a function foo( double) and you call foo( 99). The implicit conversion would convert 99 int to a double value. This is not a bad thing to do.

If you don't want a conversion to be performed, then you want the explicit value of the parameter. For example, say the class Foo, defines the constructor Foo( Bar), and Bar defines Bar( int). Calling Foo(99) will create a Foo with a Bar object, probably not what you were expecting. Before explicit showed up, any constructor that took an int was considered dangerous, because so many things could be converted to an int.
Feb 12 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

54
by: Brandon J. Van Every | last post by:
I'm realizing I didn't frame my question well. What's ***TOTALLY COMPELLING*** about Ruby over Python? What makes you jump up in your chair and scream "Wow! Ruby has *that*? That is SO...
28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
4
by: Simon Ford | last post by:
Hi All, I'm having trouble understanding exactly how I can do some specific implicit casting. There are two problems here; does anyone know what I should be doing? //---------- // (1)...
1
by: Kel Good | last post by:
Hello, I recently created a structure in C# that offers several implicit conversion, hoping I could then reference the C# project from a VB.Net project, and use this object in VB.Net. It does...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
0
by: charlee strong | last post by:
I'm seeing some strange behaviour: i have a managed wrapper class that wraps around unmanaged object, i want to convert back and forth between managed and unmanaged object, so i have overloaded...
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
8
by: jonpb | last post by:
Hi, Is it possible to define a implicit operator from base to derived types in C#? I have a Point class and would like to derive a Vector class from it and add a couple new vector related...
2
by: jimzat | last post by:
I am trying to simulate the execution of some PLC ladder logic in python. I manually modified the rungs and executed this within python as a proof of concept, but I'd like to be able to skip the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...
0
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,...
0
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...

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.