operator overloading

This is a discussion on operator overloading within the C++ Programming forums, part of the General Programming Boards category; I'm a bit confused about overloading some operators.. I did the assignment operator with no problem, but I'm trying to ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    67

    operator overloading

    I'm a bit confused about overloading some operators.. I did the assignment operator with no problem, but I'm trying to overload the addition operator without result.. what's the syntax for the addition operator?

    My code is on pastebin:

    cchar.h: http://pastebin.com/fb035fa1
    main.cpp: http://pastebin.com/f3865cf76

    stdafx is just included by visual studio as a default:

    Code:
    #pragma once
    
    
    #define WIN32_LEAN_AND_MEAN		// Exclude rarely-used stuff from Windows headers
    #include <cstdio>
    #include <cstdlib>
    #include <iostream>
    #include <windows.h>
    #include <tchar.h>
    and stdafx.cpp is:
    Code:
    // stdafx.cpp : source file that includes just the standard includes
    // Classes.pch will be the pre-compiled header
    // stdafx.obj will contain the pre-compiled type information
    
    #include "stdafx.h"
    
    // TODO: reference any additional headers you need in STDAFX.H
    // and not in this file
    Thanks for any help
    Last edited by Salem; 09-24-2008 at 11:28 AM. Reason: Make the URLs to be actually usefully clickable.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Did you just implement a character string as a linked list? Moreover, a linked list without a container, but instead a lot of individual nodes?

    That concept is beyond help, really.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,010
    operator + should always return a temp instance of itself.
    In other words, return shuld be cchar and returned value a temporary of *this where you've added the right-hand side.
    But you should also pay heed to what cornedbee says.
    Your implementation is... troublesome to say the least.

    Oh and put the implementation in a separate source file instead of inside the header.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    nevermind I got it.. it's not really useful, I just wanted to play around with pointers, classes, and operator overloading.. this worked out (and I'd forgotten what a linked list was). it's not really that useful, I know. The only good part is you don't have to memalloc it, and the fact that it supports better operators than a char*.. I hate using strings.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    How about std::string?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    strings suck.. too high-level, very annoying to use.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Huh? If std::string is too high-level, what are you looking for?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Wow, if you managed to fix that you are my superhero Not only do you implement a string as a linked list but string actually is nothing but a linked list node!

    I wonder about this line in your code:
    Code:
    tests+"hello";
    Given normal operator semantics apply this shouldn't do anything, or does it? Just like the following doesn't do anything:

    Code:
    int n = 0;
    n + 10;
    //n still 0
    I also wonder about your operator=. Why does it seem like you are trying to assign to the right-hand value? With int's it would look like this?
    Code:
    int n = 0;
    10 = n;
    //n now equals 10
    There's probably much much more weirdness going on. You should probably test all your functionality more thoroughly. And operator << and >> wouldn't hurt either for input and output.
    Last edited by anon; 09-24-2008 at 04:12 PM.
    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).

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    string has too much interface and not enough down-in-the-dirt manipulation capacbilites.. I use char*.

    and btw for some reason the + operator stores the result back in the left-hand value, very strange. Oh and the = thing works, the argument passed is the right side of the = sign, and then it gets stored in the left side.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    How so, RoboGymnast? I mean in what way do you find the STL strings lacking? I think if look into stringstreams you may be pleasantly surprised to find that your class has nothing on strings and stringstreams at all.

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    again, I wasn't really intending to use the class, as I said earlier. I'm sticking to char*. I used strings for a while and it was quite annoying, char* are very annoying in their own ways too, but I like indexing them as arrays, and it works better with libraries that use char* as parameters for their functions

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can do that with an STL string though... There are some things you cannot do, such as be changing the internal string buffers. But overall there is no need to do that with an STL string.

  13. #13
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    std::strings can also be indexed the same way as char*. The use an array. They are really a char* with some additional features

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    STL string...? whatev I'm sticking to char*.. it works fine for me

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Just to avoid another "but" I will conceed that you are entitled to both a style and opinion and none of us can force you to change either. Nor do any of us really want to. If you mess with strings enough you may find some advantage to the string class.

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

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