Generic class that accepts C++ string & C style string -
so know: c style string character array end "null"
c++ style string is:
string s = "some text";
so accept both c++ string , c style string, i'm pretty sure have use c++ template.
i'm trying write class , method returns second character of string c++ or c style string. on right track?
template <class t> class mystring { t pointer; public: mystring(t input) { pointer = input } char getsecondletter() { t temp = pointer; temp++; return temp; } }; int main () { mystring<i dont know put> myobject("hello"); cout << myobject.getsecondletter(); return 0; }
p.s. programmers use 't' template class datatype?
there no need, c string implicit convert std::string
char getsecondletter( const std::string & s ) { return s[1]; } const char *c_str = "hello"; std::string str = "world"; getsecondletter( c_str ); // e getsecondletter( str ); // o
Comments
Post a Comment