Reputation: 4687
I was reading a book on c++ when I encountered the following example:
#include <iostream>
#include <string>
using namespace std;
int main () {
const char *message = "how do you do\n";
string s(message);
cout << s << " and its size:" << s.size() << endl;
}
I wanted to know what exactly does it do. How can we pass a variable inte another variable as done in s(message)? Thanks in advance
Upvotes: 6
Views: 9739
Reputation: 1
I think you miss understanding something about string objects. You can declare and assign values to them in different ways, and it's considered a user-defined variable, which has member data and function members. here are three different ways: string word1 = "Game" ()dont forget this is a statme string word2 ("Over") string word3 (3, ’!’) so you could for example say this : a new variable phrase you could relate this one to the previous data type or (variable type ) that I created. string phrase = word1 + " " word2 + word3
Upvotes: 0
Reputation: 96119
The string 's' is created with "how do you do.." copied into it.
Think of it as
string s;
s = "how do you do\n"
It's called a constructor (sometimes abbreviated "ctor") if you want to read about it.
You can also use the ctor syntax to initialise any built in type although it's less commonly seen.
int count=10;
int count(10);
Upvotes: 1
Reputation: 46607
s(message)
actually calls the constructor of std::string
, which constructs a new object of this type from the given character array pointed to by message
. s
is just an arbitary name given to the string object. std::string
is C++'s idiomatic object for working with strings, it is usually preferred over raw C strings.
Consider this simple sample:
// Declare a fresh class
class A {
public:
// a (default) constructor that takes no parameters and sets storedValue to 0.
A() {storedValue=0;}
// and a constructor taking an integer
A(int someValue) {storedValue=someValue;}
// and a public integer member
public:
int storedValue;
};
// now create instances of this class:
A a(5);
// or
A a = A(5);
// or even
A a = 5;
// in all cases, the constructor A::A(int) is called.
// in all three cases, a.storedValue would be 5
// now, the default constructor (with no arguments) is called, thus
// a.storedValue is 0.
A a;
// same here
A a = A();
std::string
declares various constructors, including one that accepts a const char*
for initialization - the compiler automatically chooses the right constructor depending on the type and number of the arguments, so in your case string::string(const char*)
is choosen.
Upvotes: 5
Reputation: 4838
string is an object that makes it easier to work with collections of characters than using arrays. Eg its easy to add two strings together by using +
string a="my name is ";
string b="frustrated coder";
string c= a+b;//c="my name is frustrated coder"
cout<<c;
as for the question,
How can we pass a variable inte another variable as done in s(message)?
Answer: using the assignment operator '=' eg
string a="frustrated coder";
string b, c, d;//create strings b, c, and d
b=c=d=a;//basic usage of variables
here variable a is assigned to d,c, and b. I don't know if this is what you want man. :)
Upvotes: 1
Reputation: 72271
string s(message);
is a declaration of variable s
with type string
. It also supplies an argument list (message)
to be used in the constructor used to create object s
.
std::string
has a constructor similar to
string(const char* s);
so this is the constructor used to create s
.
Upvotes: 1
Reputation: 29450
In this case you are calling the string constructor function and passing a const char*
as an argument. Look here for a reference to all the different constructors.
Upvotes: 1
Reputation: 1839
The line:
string s(message);
is how we construct and initialize a variable in C++. You can also write, for instance:
int i(5);
Upvotes: 1
Reputation: 79893
That is actually one of the constructors of std::string
.
In C++, you can create objects a few different ways.
std::string s = "Hello"; // implicit constructor using const char *
std::string s = std::string("Hello"); // invoke the const char* constructor of std::string
std::string s("Hello"); // another way to do the stuff above
There are more ways than that, but just to demonstrate how you could create this std::string
object.
Upvotes: 1