0
it is possible to pass string from string class as pointer to function?
example:
void separaStr(string *modulo, satring *nmodulo, *digito){}
0
it is possible to pass string from string class as pointer to function?
example:
void separaStr(string *modulo, satring *nmodulo, *digito){}
1
I assume you’re asking if it’s possible to move to a function, a pointer to an Std:string.
Yes, it is possible. The function signature would be, for example:
int funcao(std::string *str)
However, as a rule, it suggested that whenever the function argument is necessary (it cannot be null
) and if the function does not hold a pointer or otherwise change the property/ownership (Ownership) of the argument, replace the use of a pointer by the use of a reference.
The use of the reference allows, as with the pointer, to change the string value and in terms of syntax the difference in the use of the parameter would be
str.membro (str é uma referência)
instead of
str->membro (str é um apontador)
One of the benefits of using a reference is the fact that the reference clearly expresses the condition that the function parameter cannot be null.
In a larger project this could, for example, help to easily identify points in the code where it is necessary to verify that the argument is null and the cases where to perform this validation is unnecessary.
The signature of the function that receives a string passed by reference would be:
int funcao(std::string &str)
or
int funcao(const std::string &str)
for cases where the parameter is not changed in the function.
A maxim I try to follow whenever I program in C++ is:
Browser other questions tagged c++ string pointer
You are not signed in. Login or sign up in order to post.
thank you very much, I’ve been dusting 1bit for a '&'.
– Alisson Oliveira