By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,737 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

help for concatenation of string and object.

P: 6
Hi,
this is the code to print s.

Expand|Select|Wrap|Line Numbers
  1. main() {
  2. sample s1(10,20,30);
  3. char *s;
  4. s="Hello"+s1;
  5. }
  6.  
  7.  friend char * operator +(char *s,sample &b) {
  8.         char *s1;
  9.         s1 = new char[50];
  10.         char *s2;
  11.         s2 = new char[10];
  12.         strcpy(s1,s);
  13.         strcat(s1,"Width");
  14.         sprintf(s2,"%g",b.width);
  15.         strcat(s1,s2);
  16.         strcat(s1,"Height");
  17.         sprintf(s2,"%g",b.height);
  18.         strcat(s1,s2);
  19.         strcat(s1,"depth");
  20.         sprintf(s2,"%g",b.depth);
  21.         strcat(s1,s2);
  22.         delete [] s2;
  23.  
  24.         return s1;
  25.         delete []s1;
  26.     }
  27.  
Could anybody help me for optimization?
May 31 '07 #1
Share this Question
Share on Google+
8 Replies


AdrianH
Expert 100+
P: 1,251
Hi,
this is the code to print s.

Expand|Select|Wrap|Line Numbers
  1. main() {
  2. sample s1(10,20,30);
  3. char *s;
  4. s="Hello"+s1;
  5. }
  6.  
  7.  friend char * operator +(char *s,sample &b) {
  8.         char *s1;
  9.         s1 = new char[50];
  10.         char *s2;
  11.         s2 = new char[10];
  12.         strcpy(s1,s);
  13.         strcat(s1,"Width");
  14.         sprintf(s2,"%g",b.width);
  15.         strcat(s1,s2);
  16.         strcat(s1,"Height");
  17.         sprintf(s2,"%g",b.height);
  18.         strcat(s1,s2);
  19.         strcat(s1,"depth");
  20.         sprintf(s2,"%g",b.depth);
  21.         strcat(s1,s2);
  22.         delete [] s2;
  23.  
  24.         return s1;
  25.         delete []s1;
  26.     }
  27.  
Could anybody help me for optimization?
Optimisation? How can you optimise something that doesn't work? The friend function is inside of a class? I'm pretty sure that friend injection is a deprecated feature in C++. If not, then what is it a friend of?

Other than that, it looks like quite a mess. What are you attempting to do, and what problems are you encountering?


Adrian
May 31 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Yes, and may I ask why you are not using a string and a strignstream object. All this works has been done for you. Why re-invent the wheel?

Expand|Select|Wrap|Line Numbers
  1. stringstream str;
  2.  str << " Hello" << 10 << 20 << 30;
  3.  
  4. string result;
  5. str >> result;
  6.  
May 31 '07 #3

AdrianH
Expert 100+
P: 1,251
Yes, and may I ask why you are not using a string and a strignstream object. All this works has been done for you. Why re-invent the wheel?
I assumed it was part of the assignment.


Adrian
May 31 '07 #4

P: 6
Optimisation? How can you optimise something that doesn't work? The friend function is inside of a class? I'm pretty sure that friend injection is a deprecated feature in C++. If not, then what is it a friend of?

Other than that, it looks like quite a mess. What are you attempting to do, and what problems are you encountering?


Adrian
I used friend function because i need to make the statement char*s="Hello"+s1;
work. thats why i overloaded + using friend functions because the parameters iam passing are different.

I also overloaded << to output the object s1. My aim is to see how string class is implemented.and to test the overloading concepts etc..
Jun 1 '07 #5

P: 6
Yes, and may I ask why you are not using a string and a strignstream object. All this works has been done for you. Why re-invent the wheel?

Expand|Select|Wrap|Line Numbers
  1. stringstream str;
  2.  str << " Hello" << 10 << 20 << 30;
  3.  
  4. string result;
  5. str >> result;
  6.  

I want to see how string class is implemented internally. I implemented using string class.In the code above you are taking primitive int type,but my aim is to see for objects.
Jun 1 '07 #6

AdrianH
Expert 100+
P: 1,251
I used friend function because i need to make the statement char*s="Hello"+s1;
work. thats why i overloaded + using friend functions because the parameters iam passing are different.

I also overloaded << to output the object s1. My aim is to see how string class is implemented.and to test the overloading concepts etc..
Yes, but what I am saying is that you cannot declare a friend outside of a class and you’re not supposed to declare a friend with its body inside of one (well if I understand friend injection correctly, your case could work under the new standard, but you will start getting into trouble when you start doing this using templated classes so be careful, see this article for more info).


Adrian
Jun 1 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
I want to see how string class is implemented internally. I implemented using string class.In the code above you are taking primitive int type,but my aim is to see for objects.
Why?

Just write an operator << for your classes and you are done.
Jun 1 '07 #8

AdrianH
Expert 100+
P: 1,251
Why?

Just write an operator << for your classes and you are done.
Yes, and by doing it with ostream as your left hand side (first) parameter, it would work for stringstream too since it inheirits from ostream.


Adrian
Jun 1 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.