Results 1 to 15 of 15

Thread: operator overloading

  1. #1
    Sc0rp
    Guest

    Talking operator overloading

    I'm writing my own String class as practice, and I wnated to know something:

    I'm overloading operator+ so I could do:
    Code:
    myString = myString2 + "abcdef";
    myString = myString2 + 'k';
    myString = myString2 + myString3;
    How can I overload this operator so I could do stuff like:
    Code:
    myString = "abcdef" + myString2
    myString = 'p' + myString2
    Thanks in advance.

  2. #2
    Megatron
    Guest
    To add 2 of your classes, you could use:
    Code:
    strclass& operator+ (strclass &str )
    {
        strcat(strString, str.strString);
        return *this;
    }
    
    // Usage: (assuming you overloaded '=')
    strclass one;
    strclass two;
    
    one = "test";
    two = "test";
    
    strclass three  = one + two;

  3. #3
    Sc0rp
    Guest
    I know how to do that.
    That's not the problem.
    The problem is doing:
    Code:
    String myStr = "abcdef" + myOtherStr;

  4. #4
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    they will automatically cast to the string class if you overload assignment operator
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  5. #5
    Sc0rp
    Guest
    I have these operators overloaded so far:
    Code:
    char & operator [](ulong) const;				// String[x] - returns a reference to the char at offset x
    const char* operator ()() const;				// default operator - Same as GetString()
    String& operator =(const String&);				// String1 = String2
    String& operator =(const char*);				// String1 = "abcde"
    String& operator =(char);					// String2 = 'x'
    String operator +(const char*);					// String3 = String2 + "abcdef"
    String operator +(char);					// String3 = String2 + 'k'
    String operator +(const String&);				// String3 = String2 + String1
    When I try to do:
    Code:
    #include <iostream>
    #include "scorpstr.h"
    using namespace std;
    
    int main()
    {
    	String myStr1 = "World...";
    	String myStr4 = "Hello " + myStr1;
    
    	cout << myStr4() << endl;
    	return 0;
    }
    I get these errors:
    Code:
    --------------------Configuration: String - Win32 Debug--------------------
    Compiling...
    main.cpp
    D:\Program Files\Microsoft Visual Studio\MyProjects\String\main.cpp(8) : error C2784: 'class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> __cdecl std::operator +(_D,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template a
    rgument for '' from 'char [7]'
    D:\Program Files\Microsoft Visual Studio\MyProjects\String\main.cpp(8) : error C2677: binary '+' : no global operator defined which takes type 'class String' (or there is no acceptable conversion)
    Error executing cl.exe.
    
    String.exe - 2 error(s), 0 warning(s)
    I don't know what to do.

    kedaman: Is overloading the 'equals' operator what you meant in your last post?

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    hmm i'm not sure why you are getting the first error, probably irrelevant.
    Have you set a copy constructor to take in a string? The assignment operator (yeah it's "=") is not called at the initialization line, sorry I didn't notice.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Sc0rp
    Guest
    This is the whole class declaration (if it helps):
    Code:
    class String
    {
    private:
    	char* theBuffer;
    	ulong theLen;
    
    public:
    	String();										// default constructor
    	String(ulong newLen);							// constructor receives length of string
    	String(char* newString);						// constructor intializes with string
    	String(const String&);							// copy contructor
    	~String();										// destructor
    
    	char & operator [](ulong) const;				// String[x] - returns a reference to the char at offset x
    	const char* operator ()() const;				// default operator - Same as GetString()
    	String& operator =(const String&);				// String1 = String2
    	String& operator =(const char*);				// String1 = "abcde"
    	String& operator =(char);						// String2 = 'x'
    	String operator +(const char*);					// String3 = String2 + "abcdef"
    	String operator +(char);						// String3 = String2 + 'k'
    	String operator +(const String&);				// String3 = String2 + String1
    	
    	ulong GetLen() const { return theLen; }			// returns the length of the string
    	char* GetString() const { return theBuffer; }	// returns the string as a char pointer
    };

  8. #8
    Sc0rp
    Guest
    BTW, I don't think it has anything to do with constructors:
    Code:
    int main()
    {
    	String myStr1 = "World...";
    	String myStr2;
    	myStr2 = "Hello " + myStr1;
    
    	cout << myStr2() << endl;
    	return 0;
    }
    This gives me the same errors as in the above post.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    okay, then there's something else going, on
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    global maybe

    maybe you should discard all operator+ in your class and use a global addition operator instead

    String operator+(String a, String b);

    and let the constructors convert the operands to individual Strings
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  11. #11
    Sc0rp
    Guest
    I want it to be encapsulated in the class.
    Isn't there any way to get it working within the class?

  12. #12
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    The expression is beyond the class anyway, so it's a logical choise.
    Hmm, I'll try to look into something anyway.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  13. #13
    Sc0rp
    Guest
    Thanks a lot.

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    global operation seems to be a common solution to this polymorphic issue. Say you assign a datatype with String constructor, the following expression: char* + char*. How is the compiler suppose to know the expression has to be converted to String when there's no String type specified? The convertion to the type assigned is not explicit enough since any arguments passed in overloaded constructors can be implicitely converted, so there's a ambiguety issue. Therefore the compiler doesn't even try convert the whole expression to String. A global overloaded operator would convert char*+char*but on the other hand would suggest ambigueties with other global overloaded operators with both hand argument types containing a char* constructor. But thats a small chance and if that ever happens you can explicitely use type casting.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  15. #15
    Sc0rp
    Guest
    I guess I'll have to go with the global approach.
    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width