Pages: [1] :: one page |
|
Author |
Thread Statistics | Show CCP posts - 0 post(s) |
Kaeten
Hybrid Syndicate
|
Posted - 2007.04.23 21:23:00 -
[1]
Got a challenge for you c++ people. I've been doing this for some time now and I'm confused. I'm supposed to with help of "friend" or so to make my own operator.
I am to attach two strings together into one. Like below.
char a[10]="hey_",b[10]="du_",c[}=""; c=a+b;
With the help of operator+(), anyone willing to help?
Pwnage PvP Recruitment \m/ Metal Head \m/ |
Darth Vroevl
Base Delta Zero
|
Posted - 2007.04.23 22:43:00 -
[2]
Joining the strings together is as easy as c=strcat(strcpy(new char[strlen(a)+strlen(b)+1], a), b); , but I'm pretty sure you cant override the built in operators - at least my compiler insists that one of the parameters of + be a userdefined class.
You can probably work around it with something like
class p { public: p(const char *a) : _p(a) {} char *operator+(const char *b) {return strcat(strcpy(new char[strlen(_p)+strlen(b)+1], _p), b);}
private: const char *_p; };
...
c = p(a) + b;
Though its a bit akward. |
Barbarellas Daughter
Lonely Barbarella
|
Posted - 2007.04.24 01:41:00 -
[3]
Originally by: Kaeten char a[10]="hey_",b[10]="du_",c[}=""; c=a+b;
"hey_du_" ??
|
Kaeten
Hybrid Syndicate
|
Posted - 2007.04.29 14:42:00 -
[4]
Originally by: Barbarellas Daughter
Originally by: Kaeten char a[10]="hey_",b[10]="du_",c[}=""; c=a+b;
"hey_du_" ??
Yea I know it sounds stupid but yea.
Pwnage PvP Recruitment \m/ Metal Head \m/ |
Kaeten
Hybrid Syndicate
|
Posted - 2007.04.29 14:43:00 -
[5]
Originally by: Darth Vroevl Joining the strings together is as easy as c=strcat(strcpy(new char[strlen(a)+strlen(b)+1], a), b); , but I'm pretty sure you cant override the built in operators - at least my compiler insists that one of the parameters of + be a userdefined class.
You can probably work around it with something like
class p { public: p(const char *a) : _p(a) {} char *operator+(const char *b) {return strcat(strcpy(new char[strlen(_p)+strlen(b)+1], _p), b);}
private: const char *_p; };
...
c = p(a) + b;
Though its a bit akward.
I was also to use the help if OS (outstream) operator=()
Pwnage PvP Recruitment \m/ Metal Head \m/ |
Vmir Gallahasen
Gallente Captain Morgan Society Privateer Alliance
|
Posted - 2007.04.29 20:56:00 -
[6]
Edited by: Vmir Gallahasen on 29/04/2007 20:53:08
Originally by: Kaeten I was also to use the help if OS (outstream) operator=()
If you're asking how to do that ...? Not completely sure what you mean, using operator= with outstream? Here's how you do ostream with <<, perhaps it will give you some ideas.
friend ostream& operator<<(ostream& aOs, const YourClass& aObj);
Make sure you return a reference to ostream when you're done with it.
-Vmir
|
Kaeten
Hybrid Syndicate
|
Posted - 2007.04.29 21:05:00 -
[7]
Originally by: Vmir Gallahasen Edited by: Vmir Gallahasen on 29/04/2007 20:53:08
Originally by: Kaeten I was also to use the help if OS (outstream) operator=()
If you're asking how to do that ...? Not completely sure what you mean, using operator= with outstream? Here's how you do ostream with <<, perhaps it will give you some ideas.
friend ostream& operator<<(ostream& aOs, const YourClass& aObj);
Make sure you return a reference to ostream when you're done with it.
-Vmir
cra psorry I was confused. Not outstream that was another thing. It was just operator=()
Pwnage PvP Recruitment \m/ Metal Head \m/ |
Turiya Flesharrower
Beagle Corp
|
Posted - 2007.04.29 21:11:00 -
[8]
string a = "hey_"; atring b = "du_"; string c = a + b; cout << c << endl;
Or, if you really need to use char arrays:
char a[10] = "hey_"; char b[10] = "du_"; char c[20]; strcpy(c, a); strcat(c, b);
-----
|
Pilk
Axiom Empire
|
Posted - 2007.04.30 06:46:00 -
[9]
Edited by: Pilk on 30/04/2007 06:42:44 Edit: [code] tagging Depending on just how braindead your precompiler is, you can always do it the cheating way:
#define + <<
char a[10]="hey_",b[10]="du_"; cout << a + b; 100% guaranteed to make most compilers explode, though, to say nothing of what it might do to the rest of your program even if it did work.
--P
|
Kaeten
Hybrid Syndicate
|
Posted - 2007.04.30 15:06:00 -
[10]
Originally by: Turiya Flesharrower string a = "hey_"; atring b = "du_"; string c = a + b; cout << c << endl;
Or, if you really need to use char arrays:
char a[10] = "hey_"; char b[10] = "du_"; char c[20]; strcpy(c, a); strcat(c, b);
that is cheating
I am to make my own .h and .cpp file. The default use for + is to add two ints together 5 + 5. I am to remake the operator so that it adds two strings together. I am to do so in my .h and .cpp file. In other words make my own functino for "+" and also to change "=" so that it displays char + char as a third char. In this case C. Hope this helps lol.
Pwnage PvP Recruitment \m/ Metal Head \m/ |
|
|
|
|
Pages: [1] :: one page |
First page | Previous page | Next page | Last page |