-
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
-
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.
-
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.
-
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.
-
-
strings suck.. too high-level, very annoying to use.
-
Huh? If std::string is too high-level, what are you looking for?
-
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:
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.
-
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.
-
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.
-
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
-
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.
-
std::strings can also be indexed the same way as char*. The use an array. They are really a char* with some additional features
-
STL string...? whatev I'm sticking to char*.. it works fine for me
-
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.