Overriding '+' operator

This is a discussion on Overriding '+' operator within the C++ Programming forums, part of the General Programming Boards category; Hello, everyone. I was wondering is it possible to override the addition operator so that it will take two constants ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Overriding '+' operator

    Hello, everyone.

    I was wondering is it possible to override the addition operator so that it will take two constants of type char*.

    I tried writing the following code but it only gave me an error message stating "'operator +' must have at least one formal parameter of class type".

    Code:
    char* operator+(const char* left, const char* right)
    {
    	char* c, *d;
    	d = new char[strlen(left) + 1];
    	strcpy(d, left);
    	c = new char[strlen(left) + strlen(right) + 1];
    	c = strcat(d, right);
    	return c;
    }
    So, is it fundamentally impossible to override this operator outside a user defined class, or is there a way to do it?

    If so, how?

    Thanks.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Why do you want to overload it with two const char *s? That already exists, and that's what the compiler's complaining about.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Oops, didn't explain enough in my post.

    I just wanted to try and create something like the string handlers you find in Visual Basic.

    In visual basic you can do this:
    Code:
    Dim S as String
    S = "Hello" + "World!"
    S = "Hello" & "World!"
    Either '+' or '&' would function as concatenation operators if they were passed strings.

    I know you can't do that with character pointers in c++. But can you override them to act like the Visual Basic concatenators if the operands were character pointer constants?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I was wondering is it possible to override the addition operator so that it will take two constants of type char*.
    No, that is why you are getting this error:
    I tried writing the following code but it only gave me an error message stating "'operator +' must have at least one formal parameter of class type
    You can only overload operators for members of a user defined class:
    http://geneura.ugr.es/~jmerelo/c++-f...html#faq-26.10
    Last edited by 7stud; 11-14-2005 at 03:21 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Just use the C++ string class and its operator+
    Code:
    std::string S;
    S = "Hello" + std::string("World!");
    // or
    S = std::string("Hello") + "World!";
    // or the best option for small additions:
    S = "Hello";
    S += "World";

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Thanks. Now I know better.

    If C++ let you redefine the meaning of operators on built-in types, you wouldn't ever know what 1 + 1 is: it would depend on which headers got included and whether one of those headers redefined addition to mean, for example, subtraction.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ...or
    Code:
    string char1 = "o";
    string char2 = "k";
    cout<<char1 + char2<<endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overriding operator new/delete?
    By Elysia in forum C++ Programming
    Replies: 13
    Last Post: 02-25-2008, 11:10 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Overriding = operator.
    By Strahan in forum C++ Programming
    Replies: 4
    Last Post: 09-11-2001, 03:26 PM

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