There is a class (structure) external and internal (inside the first). Made to overload the operator [][] . In the task, it was necessary to cut the string "from" and "to" by this operator.
I in Maine display a lot of information. INFA displayed correctly and without oddities. BUT. If I declare std::cout << nTemp in the substring structure, then the output immediately changes. All the lines that I wrote in Maine disappear, and in general, it seems to me that the program is starting to behave very suspiciously.
struct String { struct Substring{ //ВТОРАЯ часть //-------------- const String operator [](int index) const{ //Здесь проводим аналогичные операции, как и со String и возвращаем уже конечный String int nTemp = index - nPastIndex; char SecStr[nTemp+1]; SecStr[nTemp]='\0'; //std::cout<<nTemp; // Тут проблемный участок strncpy(SecStr, str, nTemp); String pStr(SecStr); return pStr; } size_t nPastIndex; size_t size; char *str; char *secstr; }; //ПЕРВАЯ часть //-------------- const Substring operator [](const int index)const{ //Определяем будущий размер массива и определяем его int nTemp = size - index; char SecStr[nTemp]; SecStr[nTemp]='\0'; //Копируем строку через string в наш массив и передаем этот массив конструктору, // который добавляет его в свои поля. Возвращаем объект в следующую кв. скобку [] // - то есть в Substring (он выше) std::string const szNewStr(str); strcpy(SecStr, szNewStr.substr(index, size).c_str() ); Substring NewStr(SecStr,index); return NewStr; } size_t size; char *str; char *secstr; }; //main для тестов //----------------- int main(){ const String s("hello world."); String const hello("hello world."); std::cout<<hello.str<<" "<<strlen(hello.str)<<std::endl; String const hell = hello[0][4]; std::cout<<hell.str<<" "<<strlen(hell.str)<<std::endl; String const ell = hello[1][4]; std::cout<<ell.str<<" "<<strlen(ell.str)<<std::endl; String const ll = hello[0][0]; std::cout<<ll.str<<" "<<strlen(ll.str)<<std::endl; String const l = hello[3][9]; std::cout<<l.str<<" "<<strlen(l.str)<<std::endl; if(strcmp(s[1][4].str, "ell")==0){ std::cout<<"True"<<std::endl; } return 0; }
Stringstructure that does not contain a destructor and a copy constructor - do you know that you have a memory leak? - VladDSubstringconstructor look like? It is important. - VladD