473,414 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

clarification needed regarding string& replaceLastStar(string& s )

1
hi,
i am using
language:c++
platform:linux


I am new to c++.
Can u please guide me by giving the meaning of string&.
if we give
Expand|Select|Wrap|Line Numbers
  1. string& replaceLastStar(string& s )
here string& S
means.


Thanks & regards
rekha
Jul 20 '07 #1
2 1760
Meetee
931 Expert Mod 512MB
hi,
i am using
language:c++
platform:linux


I am new to c++.
Can u please guide me by giving the meaning of string&.
if we give
Expand|Select|Wrap|Line Numbers
  1. string& replaceLastStar(string& s )
here string& S
means.


Thanks & regards
rekha
String & s means you are passing reference ( address ) of String s to the function replaceLastStar.

Regards
Jul 20 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
This is not correct:
String & s means you are passing reference ( address ) of String s to the function replaceLastStar.
When you pass the address of a string, your function has a pointer argumment:
Expand|Select|Wrap|Line Numbers
  1. string* replaceLastStar(string* s );
  2.  
  3. int main()
  4. {
  5.      string data;
  6.      stirng* ptr = replaceLastString(&data)l;
  7. }
  8.  
The function works with the address of datain order to change data. The argument s is &data.

When you pass a string by reference, your function has a reference argumment:
Expand|Select|Wrap|Line Numbers
  1. string& replaceLastStar(string& s );
  2.  
  3. int main()
  4. {
  5.      string data;
  6.      replaceLastString(data)l;
  7. }
  8.  
This time name data and the name s are two names for the same string. Inside the function a change to s is also a change to data since these are the same string. Passing by reference avoids making a copy of the object and also avoids having to use a pointer.
Jul 22 '07 #3

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

Similar topics

5
by: selder21 | last post by:
Hello, I have a class with constructor taking a const string&. Now i want to call this constructor with a string literal. Because this is of type char* there are overload resolution conflicts....
7
by: BlueDragon | last post by:
I don't know enough math to demonstrate that any numerical operation with a null should yield a null; although I would guess that it's true. I just don't buy it, however, when dealing with strings...
6
by: JSheble | last post by:
I realise .NET has all these great objects, specifically strings, but is it really necessary to create a string variable (object) just to compare two string values? For example, I'm looking at...
12
by: Tee | last post by:
String Builder & String, what's the difference. and when to use which ? Thanks.
10
by: lchian | last post by:
Hi, For two stl strings s1 and s2, I got different results from strcmp(s1.c_str(), s2.c_str()) and s1.compare(s2) can someone explain what these functions do? It seems that strcmp gives...
2
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to...
3
by: sernamar | last post by:
Hi, I have the following base class class UnitOfMeasure { public: //... std::string& GetName() {return _uomName;}; //... protected:
3
by: asclearuc | last post by:
Hello Is it possible to use map<string&, string&>? Why I need it. I have a large amount of data obtained from XML file. I should do processing of this data. The processing takes many...
8
by: Edson Manoel | last post by:
I have some C++ unmanaged code that takes std::string& arguments (as reference), and fills them (possibly growing the string). I want to call this code through PInvoke (DllImport), possibly using...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.