How would I return a string

Please can you tell me how would I return a string to a function ?
Last edited on
Same as any other type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>

std::string func()
{
    return "qwerty";
}

int main()
{
    std::cout << func() << '\n';

    return 0;
}
Please can you tell me what this std::string means ? Sorry I Am a beginner !
string is a standard C++ class. It's one of the provided, pre-defined types of objects you can use. To make use of it, you need to include the correct header file, like this:

#include <string>

As far as you're concerned, it's just another kind of object you can make use of, like int and double.

string is part of the std namespace. namespaces are a way of arranging types and functions into groups so that clashes (different types that happen to have the same name because they were made by different people) aren't a problem. You can read about namespaces here: http://www.cplusplus.com/doc/tutorial/namespaces/
Sorry I didn't understood What is std:: ?
std:: is the name of a namespace. std is the namespace commonly used by facilities provided to you as standard in the C++ language.
What is std:: ?

First , you need to know what a namespace is.
http://www.cplusplus.com/doc/tutorial/namespaces/

Second, you need to know when to use a namespace and when not to use a namespace.
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice


Topic archived. No new replies allowed.