STL developers lazy or did I miss something?

Reply

Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

STL developers lazy or did I miss something?

 
0
  #1
Jun 9th, 2005
string a = "A";
string b = "B";
string c = a + b;
string d = "D" + a;
string e = a + "D";
string f = "F" + "F";


Why is the last line not something the string class can handle if it can handle cases a, d, and e?
line 73: Error: The operation "const char* + const char*" is illegal.

(Also, is this something that I could add myself? By developing another + operator for string?)
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: STL developers lazy or did I miss something?

 
0
  #2
Jun 9th, 2005
string f = "F" + "F";
The last one is operator+ for const char *, like it says, not for strings. You would be trying to overload a builtin, not the string. [Or something to this affect, I believe. The operation on the right happens before the assignment.]
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: STL developers lazy or did I miss something?

 
0
  #3
Jun 9th, 2005
I got you now - it's not even under the 'string' class developers as it would be the char * operator that has to be modified not the string. (So I guess to answer my own question, 'yes, I missed something')

So if I want to do something like that, would require:

string f = "F";
f+="F";

correct?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: STL developers lazy or did I miss something?

 
0
  #4
Jun 9th, 2005
I believe so.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: STL developers lazy or did I miss something?

 
0
  #5
Jun 9th, 2005
>> So I guess to answer my own question, 'yes, I missed something'
Yes, but it's an easy mistake to make. Since string literals are of type const char *, and it's not possible to overload built-in data types, "F" + "F" is trying to add two addresses, which is an ambiguous, and thus, illegal, operation.

>> correct?
That would work, but a somewhat more concise method is:
  1. std::string("F") + "F"
or
  1. "F" + std::string("F");
since the + operator allows the string object on either side of the expression.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC