What are implicit/explicit overloading operators

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dragon1000
    New Member
    • Oct 2006
    • 2

    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
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • dragon1000
      New Member
      • Oct 2006
      • 2

      #3
      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

      Comment

      • vinodV
        New Member
        • Feb 2007
        • 4

        #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.

        Comment

        • vinodV
          New Member
          • Feb 2007
          • 4

          #5
          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.

          Comment

          • RRick
            Recognized Expert Contributor
            • Feb 2007
            • 463

            #6
            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.

            Comment

            Working...