overloaded operators

Discussion in 'Programmer's Corner' started by mentaaal, Mar 3, 2009.

  1. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Hey guys, quick question, I am trying to overload the = operator as in operator=() but I am getting errors when I try to do it globally. Doing it privately yields no errors at all.

    For simplicity i have included only the header file but the definitions can be supplied if needed.

    Code ( (Unknown Language)):
    1. #ifndef PRACTICE_H
    2. #define    PRACTICE_H
    3.  
    4. #include <iostream>
    5. #include <string>
    6. using namespace std;
    7.  
    8. class practice
    9. {
    10. public:
    11.     practice(const char[] = "empty");
    12.     practice (const practice &);
    13.     ~practice();
    14.  
    15.     char* reader();
    16.     void input(const char*);
    17.     void input(const string &);
    18.    
    19.     bool operator==(const practice &);
    20.     bool operator!=(const practice &);
    21.     practice & operator=(const practice &);
    22.    
    23.     friend practice & operator+(practice &, const practice &);
    24.     friend istream & operator>>(istream &, practice &);
    25.     friend ostream & operator<<(ostream &, const practice &);
    26.    
    27. private:
    28.     char *ptr;
    29. };
    30.  
    31. #endif
    32.    
    33.  
    34.  
    This method works just fine.

    Code ( (Unknown Language)):
    1. #ifndef PRACTICE_H
    2. #define    PRACTICE_H
    3.  
    4. #include <iostream>
    5. #include <string>
    6. using namespace std;
    7.  
    8. class practice
    9. {
    10. public:
    11.     practice(const char[] = "empty");
    12.     practice (const practice &);
    13.     ~practice();
    14.  
    15.     char* reader();
    16.     void input(const char*);
    17.     void input(const string &);
    18.    
    19.     bool operator==(const practice &);
    20.     bool operator!=(const practice &);
    21.    
    22.     friend practice & operator=(practice &, const practice &);
    23.     friend practice & operator+(practice &, const practice &);
    24.     friend istream & operator>>(istream &, practice &);
    25.     friend ostream & operator<<(ostream &, const practice &);
    26.    
    27. private:
    28.     char *ptr;
    29. };
    30.  
    31. #endif
    32.    
    33.  
    34.  
    THis method yields the following errors:

     
    Last edited: Mar 3, 2009
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    In your two versions of practice.h, I'm assuming the first one works fine, and the second one doesn't. It was a little hard to figure out what "this" referred to in each -- better would be to describe with "above" or "below."

    The difference between the two examples seems to be that the first (and working) has an explicit declaration for an operator= operator and the second (nonworking) makes it a friend operator. I'm very rusty on these C++ intricacies and don't have time right now to research it, so will instead just try to point you in the appropriate directions.

    There could be some other stuff going on behind the scenes in your second example. I believe that if you don't provide an operator=, you get one for free, so it could be there is some sort of confusion on the part of the compiler, which generates a default operator= and tries to generate the friend one you're declaring. It's not clear to me why you have specified friend for this operator in your second example. The basic idea, IIFC, is that you can confer friendship on another class. In two of your other operators, there is a connection to either istream or ostream, but that's not the case with your friend operator=. I'm really fuzzy about this, but I would suggest you read up on the friend concept as it applies to C++ classes.

    Also take a look to see what you can find about the default methods and operators a compiler builds for a class. These include a default (no args) constructor, a default copy construction, and I think, operator=.

    Hope this is helpful.
     
  3. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Your input is always helpful Mark. I will try to see if I can find some more info about it. I will also ask my lecturer but I wont hold thumbs. I have a strange feeling that this operator cannot be overloaded globally (using it as a friend function)

    I tried googling it to see if any others have the same problem.

    Will let you know if I find anything
     
  4. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Well i just found this on a website:

    An operator function can be either a "friend" function or a "member" function of a class. A friend function has access to all private and protected members of a class for which it is a friend. A friend function is declared using a prototype within the class, preceded by the [COLOR=blue ! important][COLOR=blue ! important]keyword[/COLOR][/COLOR] friend. The following operators cannot be overloaded using friend functions: = () [] -

    The link to the website that I found this on is:
    http://www.gamespp.com/c/introductionToCppMetrowerksLesson09.html


    Its a bit strange as to why they cant be overloaded but not to worry Mark, I was mainly just wandering if it was some obscure syntax that I was overlooking.

    Thanks
     
  5. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    I'm guessing that it was considered too insecure to offer access to everything nonpublic to a nonmember operator. Bjarne Stroustrup probably has something to say about it in The C++ Programming Language, Second Ed. As much coding in C++ as you do, this would be a good resource for you. Possibly it's out in a newer edition.
    Mark
     
  6. mentaaal

    Thread Starter Senior Member

    Oct 17, 2005
    451
    0
    Indeed, you are right, he says:
    The book which i am using is c++ how to program 5th edition by deitel and deitel which I find to be very good, although in fairness I have no comparison as its the only book I have read.
    If you reckon the above is good I will definitely give it a good attempt.
     
  7. Mark44

    Well-Known Member

    Nov 26, 2007
    626
    1
    Stroustrup designed the language, so what more can I say? I had the Deitel&Deitel book in some edition at one time, and as I recall, it was pretty good. You can probably pick up a used copy of the Stroustrup one at abebooks.com.
     
Loading...