Thread: Operator Overloading....

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Operator Overloading....

    I'm reading up on this at present and I feel that I understand how and what it does.

    My question is, is this a technique that is regularly used in programming and if so is it mainly used for abstraction reasons? Ie, making life easier for the coder in the long run as opposed to providing any essential functionality?

    Thanks.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It's regularly used, yeah. I suppose it does not provide any essential functionality no, in so far as the same thing could be accomplished with a "normal" function.

    However, it is no more or less complex than writing a "normal" function and it does provide a nicer, more standardized interface, which I'd guess is why most people use it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Thanks. So for example, if I wanted say a print function that loops through some given vector, I could instead overload << to take the ostream and vector as parameters, loop and print to the os, and then simply call be cout << vector?

    I have done something similar(albet with an object) and it does work, I was just wondering if this is the type of context it would be used?

    Code:
    void operator<<(ostream& os, Name_pairs& np)
    {
    	for(int i = 0; i<np.name.size(); ++i){
    		 os << np.name[i] << endl;
    	}
    }
    Code:
    cout << friends; // name of object passed by reference into << overload.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Cout does not have << defined for vector so that should be fine:
    Code:
    void operator<<(ostream& os, vector<int>& v) {
    	vector<int>::iterator it = v.begin(), end =  v.end();
    	while(it != end) { 
    		os << (*it++) << endl;
    	}
    }
    Otherwise one disadvantage of "overloading" is you could do exactly that (replace functionality instead of just adding some).

    I dunno how to do this with a template type for the vector (if you can do that at all).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Cout does not have << defined for vector so that should be fine
    It should return an `std:stream &'.

    Otherwise one disadvantage of "overloading" is you could do exactly that.
    If I understand what you are saying, you are wrong; source with two functions having different definitions with the same signature and scope is invalid.

    I dunno how to do this with a template type for the vector (if you can do that at all).
    I'm not sure if I get you here either, but that would be as easy as making the operator itself a template function.

    Soma

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by phantomotap View Post
    It should return an `std:stream &'.
    Oh yeah! Otherwise cout itself cannot continue. So:

    Code:
    std::ostream& operator<<(ostream& os, vector<int>& v) {
    	vector<int>::iterator it = v.begin(), end =  v.end();
    	while(it != end) { 
    		os << (*it++) << endl;
    	}
    	return os;
    }
    Quote Originally Posted by phantomotap View Post
    I'm not sure if I get you here either, but that would be as easy as making the operator itself a template function.
    The "as easy as" part I could not work out -- ie, adapting the above function to work with any <kind> of vector.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The "as easy as" part I could not work out -- ie, adapting the above function to work with any <kind> of vector.
    In exactly the same way as you would adapt any other function to work with any kind of `std::vector<???>'. That it is an operator is meaningless.

    Soma

    Code:
    template
    <
       typename element_F
     , typename allocator_F
    >
    
    std::ostream& operator << (std::ostream& os, const std::vector<element_F, allocator_F>& v) {
    	typename std::vector<element_F, allocator_F>::const_iterator it = v.begin(), end =  v.end();
    	while(it != end) {
    		os << (*it++) << std::endl;
    	}
    	return os;
    }

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also note how Soma fixed the right-hand argument to be const.
    The vector reference should be const since we're not modifying it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Thanks for that.

    Why do you need "typename" here:
    Code:
    std::ostream& operator << (std::ostream& os, const std::vector<element_F, allocator_F>& v) {
    	typename std::vector<element_F, allocator_F>::const_iterator it = v.begin(), end =  v.end();
    Last edited by MK27; 06-16-2010 at 08:27 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because const_iterator lies inside the vector class that depends on the unknown types element_F and allocator_F.
    See it this way: since we're passing unknown types (before the template is instantiated) to the vector class, we cannot guarantee that const_iterator is a class in vector<element_F, allocator_F>.
    Why? Because we could specialize vector for different types and make const_iterator a function if we wanted. So because the compiler cannot determine which of the specializations of the class, if any, it needs to look in, we need to help it by saying that it is, indeed, a type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed