6

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

1
  • Keep reading about objects and it'll all be cleared up. Commented Mar 18, 2011 at 17:30

8 Answers 8

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much! But why do we need a constructor in this case? Like integers and floating point no.s dont have initial values, same should have gone with string also right?
integers and floating point numbers are primitive types, they are undefined unless you assign something to them. std::string is an object, however - usually, objects should have a well-defined state after construction and std::string follows this rule.
@Frustrated coder. You don's strictly need it in this case - although it can be more efficient. But consider if the String() needed two values eg the text and a language code, then it's easier to do 'string("blah blah",english)' than use '='
1

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.

Comments

1

The line:

string s(message);

is how we construct and initialize a variable in C++. You can also write, for instance:

int i(5);

Comments

1

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.

Comments

1
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.

Comments

1

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. :)

1 Comment

Actually I am wondering if we are passing message into string like passing a variable as a parameter?
1

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);

2 Comments

Just to be clear, while in general the outcome of the two is the same setting that value at instantiation and initializing after instantiation can have affects on performance. For more info: parashift.com/c++-faq-lite/ctors.html
Yes they aren't exactly equivalent, otherwise the ctor paradigm would be needed, but it's close enough for the poster's needs.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.