+ 1
Return char as the Char class(my class)
I want to return a char of a std::string as reference object of my class What to do?
6 Respostas
+ 4
You wrote, I will reformulate: I want my class to return a reference object to std :: string ...
If you mean something else, be clearer.
There are practically no psychics here, I have not met ...
+ 3
char& yourClass::getStrPropCharR(int i){
return *(m_StrProp.begin() + i);
};
...
or
char* yourClass::getStrPropCharP(int i){
return (m_StrProp.begin() + i);
};
...
///////
But working with the std :: string type this way is not very good ... there will be problems when modifying a string through its native methods and through modifying a separate character .... it is better to leave std :: string and use std :: vector <char>
+ 2
try it:
class yourClass{
...
public:
std::string& getStrProp();
...
private:
std::string m_StrProp;
...
};
std::string& yourClass::getStrProp(){
return m_StrProp;
};
+ 1
Why you want to wrap a char inside your class? why can't you just return the char?
You can design your class' constructor to take a char, and store it as value of a private member. That way you can instantiate your class using the char taken off a std::string.
Maybe you need to elaborate further on the idea of this class.
0
I wrote I need return reference of an element of std::string
But std::string's elements are char
I need to convert it to MyChar class
But it still bee a reference
I mean anyone can change that element
- 3
This is not my answer