overloading operator in C++

This is a discussion on overloading operator in C++ within the C++ Programming forums, part of the General Programming Boards category; If say I want to overload the following: Code: String operator*(const String& str, int n); and my test case is: ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    overloading operator in C++

    If say I want to overload the following:

    Code:
    String operator*(const String& str, int n);
    and my test case is:

    Code:
    String ab = ab0 * 3;
    String abc = 2 * (ab + String('c'));
    do I need to write another function with a different parameter, or is there a way to just use one function definition and perform both operators?

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think you'll need another, but you can probably implement it in terms of the first:

    Code:
    String operator*(int n, const String& str)
    {
        return str * n;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    May I know the reason why?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Because in the first case, the compiler looks for a function with a parameter list that looks like const String&, int, and in the second the reverse. Since no such function exists...
    The reason for the need of two is probably technical. Such as the compiler cannot guarantee no side effects.
    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.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I see that explains, also another question about overloading unary operator.. I have the following:

    Code:
    String String::operator-(){
    	String r('x', strlen(itsPtr) + 1);
    	int j = 0;
    	for(int i = strlen(itsPtr) + 1; i >= 0; i--){
    		r[j] = itsPtr[i];
    		j++;
    	}
    	return r;
    }
    but when I do the test:

    Code:
     String x("abc");
        String y = -x;
    it says:
    s0.cc:31: error: no match for ‚operator-‚ in ‚-x‚

    definitely there's a operator-

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Sorry, but I cannot replicate your problem:
    Code:
    class String
    {
    public:
        String operator-()
        {
            return *this;
        }
    };
    
    int main()
    {
        String x;
        String y = -x;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Did you declare it properly in your prototype?
    Code:
    class Test
    {
    public:
    	Test operator - () const
    	{
    		return Test();
    	}
    };
    
    int main()
    {
    	Test test;
    	Test test2 = -test;
    }
    This works fine, as expected.
    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.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay lets get back to my first topic, when I just use that one... I get:

    error: invalid conversion from ‚int‚ to ‚const char*‚
    error: initializing argument 1 of ‚String::String(const char*)‚
    error: invalid conversion from ‚const char*‚ to ‚int‚
    error: initializing argument 2 of ‚String operator*(const String&, int)‚

    so does this mean that I must use two function signature?

    PS: I fixed the error of operator-

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    Your operator * at the beginning of the thread? If so, then there's nothing wrong with it from the look of it.
    Can you provide an example?
    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.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    here is my operator*

    Code:
    String operator*(int n, const String& str){
    	String r;
    	if (n > 0){
    		for (int i = 0; i < n; i++)
    		r = str + str;
    	}
    	else if (n < 0)
    		printf("assertion failure\n");
    	
    	return r;
    }
    my test:

    Code:
    String ab = ab0 * 3;
        String abc = 2 * (ab + String('c'));

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    The biggest problem I see is your first line. It's (String, int), but your defined operator is (int, String).
    I got this minimal example to compile (it is by no means correct):
    Code:
    class String
    {
    public:
    	String(char c) { m_str = c; }
    	String(const std::string& str): m_str(str) {}
    	String() {}
    	friend String operator + (const String& lhs, const String& rhs)
    	{
    		return lhs.m_str + rhs.m_str;
    	}
    	friend String operator*(int n, const String& str)
    	{
    		String r;
    		if (n > 0){
    			for (int i = 0; i < n; i++)
    				r = str + str;
    		}
    		else if (n < 0)
    			printf("assertion failure\n");
    
    		return r;
    	}
    
    protected:
    	std::string m_str;
    };
    
    int main()
    {
    	String ab0;
    	String ab = 3 * ab0;
    	String abc = 2 * (ab + String('c'));
    }
    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.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    that's what my original question is, would it be possible just to use one function definition to cover both (String, int) and (int, String) in overloading?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    No. You need to define two.
    But the second can just wrap the first.

    You may also want to look into Boost Operators: http://www.boost.org/doc/libs/1_42_0.../operators.htm
    It saves you from writing several operators, plus it also defines them in a two-way form: ie T, U and U, T.
    Handy stuff.
    Last edited by Elysia; 04-02-2010 at 11:49 AM.
    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.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay... some say that using operator cast will allow me to use one function definition

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Yes, but it will also allow much more than that.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Operator Overloading
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2009, 12:01 AM
  2. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  3. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  4. Overloading operator ==
    By anon in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2006, 04:26 PM
  5. overloading
    By theLukerBoy in forum C++ Programming
    Replies: 6
    Last Post: 11-04-2002, 08:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21