BoeNoe BoeNoe - 10 months ago 55
C++ Question

Asterisk in function type

I found some code online for a C++ Chat Server. And in the code below contains something I don't get

string* buildPrompt()
// Code for chat server

What is that asterisk after
? If I remove it, will the code stop working?

Answer Source

Asterisk after the string means that it is a pointer to a string object somewhere else in memory. Removing the asterisk changes the return type from the function:

string* buildPrompt();  // <-- Returns pointer to a string.
string buildPrompt();   // <-- Returns an instance of a string.

I would recommend doing some basic reading on pointers in C++, they're pretty fundamental to the language once you advance from simple "Hello, World!" programs.