Hi,
this is the code to print s. - main() {
-
sample s1(10,20,30);
-
char *s;
-
s="Hello"+s1;
-
}
-
-
friend char * operator +(char *s,sample &b) {
-
char *s1;
-
s1 = new char[50];
-
char *s2;
-
s2 = new char[10];
-
strcpy(s1,s);
-
strcat(s1,"Width");
-
sprintf(s2,"%g",b.width);
-
strcat(s1,s2);
-
strcat(s1,"Height");
-
sprintf(s2,"%g",b.height);
-
strcat(s1,s2);
-
strcat(s1,"depth");
-
sprintf(s2,"%g",b.depth);
-
strcat(s1,s2);
-
delete [] s2;
-
-
return s1;
-
delete []s1;
-
}
-
Could anybody help me for optimization?
8 1901
Hi,
this is the code to print s. - main() {
-
sample s1(10,20,30);
-
char *s;
-
s="Hello"+s1;
-
}
-
-
friend char * operator +(char *s,sample &b) {
-
char *s1;
-
s1 = new char[50];
-
char *s2;
-
s2 = new char[10];
-
strcpy(s1,s);
-
strcat(s1,"Width");
-
sprintf(s2,"%g",b.width);
-
strcat(s1,s2);
-
strcat(s1,"Height");
-
sprintf(s2,"%g",b.height);
-
strcat(s1,s2);
-
strcat(s1,"depth");
-
sprintf(s2,"%g",b.depth);
-
strcat(s1,s2);
-
delete [] s2;
-
-
return s1;
-
delete []s1;
-
}
-
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
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? -
stringstream str;
-
str << " Hello" << 10 << 20 << 30;
-
-
string result;
-
str >> result;
-
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
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..
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? -
stringstream str;
-
str << " Hello" << 10 << 20 << 30;
-
-
string result;
-
str >> result;
-
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.
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
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jonas Galvez |
last post by:
Is it true that joining the string elements of a list is faster than
concatenating them via the '+' operator?
"".join()
vs
'a'+'b'+'c'
If so, can anyone explain why?
|
by: Trint Smith |
last post by:
Ok,
I have a webform that has these checkboxes:
1. something
2. something else
3. and something else
When the user clicks on the checkbox, I want all of the selections to go
into a textbox...
|
by: Lucas Tam |
last post by:
Hi all,
I'm concatenating a large SQL string for updating a table. There are >
80,000 commands (rows) in the SQL string. VB.NET seems to be *VERY* slow at
string concatenation when the string...
|
by: Justin M. Keyes |
last post by:
Hi,
Please read carefully before assuming that this is the same old
question about string concatenation in C#!
It is well-known that the following concatenation produces multiple
immutable...
|
by: genc_ymeri |
last post by:
Hi over there,
Propably this subject is discussed over and over several times. I did google
it too but I was a little bit surprised what I read on internet when it
comes 'when to use what'.
Most...
|
by: Larry Hastings |
last post by:
This is such a long posting that I've broken it out into sections.
Note that while developing this patch I discovered a Subtle Bug
in CPython, which I have discussed in its own section below.
...
|
by: Lucky |
last post by:
hi guys!
back again with another query.
the problem is like this.
i want to print a line like this:
"---------------------------------------------"
the easiest way is to simply assign it to...
|
by: kellysgirl |
last post by:
Option Explicit On
Option Strict On
Imports System.Globalization
Public Class MainForm
Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
|
by: eBob.com |
last post by:
I am trying to understand a bit of JavaScript from
http://developer.yahoo.com/maps/simple/jspost.html (appended below). I'd
appreciate help understanding two things it. The first is the...
|
by: hcaptech |
last post by:
This is my Test.can you help me ?
1.Which of the following statement about C# varialble is incorrect ?
A.A variable is a computer memory location identified by a unique name
B.A variable's name...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |