I study with ++, there is a question:
There is a class:
#ifndef STRING_2_H #define STRING_2_H #include <iostream> class String { private: char *str; int len; static int num_strings; static const int CINLIM = 80; public: String(const char * s); String(); String(const String &); ~String(); String & operator+(const String &); String & stringlow(); String & stringup(); int has(const char) const; bool operator==(const String &); String & operator=(const String &); friend String & operator+(const char *, const String &); friend std::ostream & operator<<(std::ostream &, const String & a); friend std::istream & operator>>(std::istream &, String &); }; #endif there is an implementation of the class methods, though a bit unfinished:
#include "string2.h" #include <cstring> String::String() { str = new char[1]; *str = '\0'; len = 0; // num_strings++; } String::String(const char * s) { len = strlen(s) + 1; str = new char[len]; strcpy(str, s); // num_strings++; } String::String(const String & a) { len = a.len; str = new char[len]; strcpy(str, a.str); // num_strings++; } String::~String() { // num_strings--; //std::cout << "Dest" << num_strings << std::endl; delete [] str; } String & String::operator+(const String & a) { std::cout << "debug1"; char * buffer; buffer = new char [len + a.len + 1]; strcpy(buffer, str); String res(strcat(buffer, a.str)); delete [] buffer; std::cout << "debug1"; return res; } String & String::stringlow() { } String & String::stringup() { } int String::has(const char a) const { } bool String::operator==(const String &) { } String & String::operator=(const String & a) { if(this == &a) { return *this; } //std::cout << a; std::cout << "debug10"; len = a.len; delete [] str; str = new char[len]; strcpy(str, a.str); return *this; } String & operator+(const char *a, const String & b) { std::cout << "debug2"; } std::ostream & operator<<(std::ostream & os, const String & a) { os << a.str; return os; } std::istream & operator>>(std::istream & is, String & a) { char *buffer; buffer = new char[String::CINLIM]; is.getline(buffer, String::CINLIM); a = buffer; delete [] buffer; return is; } The main program is:
#include <iostream> using namespace std; #include "string2.h" int main() { String s1(" and I am a C++ student."); String s2 = "Please enter your name: "; String s3; cout << s2; cin >> s3; std::cout << "cout"; s2 = "My name is " + s3; cout << "Bye\n"; return 0; } There is a compiler output
~ / workspace / learning_c ++ $ ./a.out Please enter your name: Bla terminate called after throwing an instance of 'std :: bad_alloc'
what (): std :: bad_alloc debug10coutdebug2debug10 Emergency shutdown (memory dump made)
It is not clear where the error is.
The following is not clear: we have the string s2 = "bla bla" + s3; Why is the operator called first in the console output = when the priority is lower than +? As I understand it, in c ++ there are conversion constructors - one of them is String::String(const char * s) . Why does the compiler require precisely a friendly function with charms and strings in arguments, but does not convert charms into a string and from there into the + (string) operator?
>>operator goes when the operation is=. If we really take up classes - trace - and find the error quickly. I can say for sure that the operator>>first called in the stringcin >> s3;- nick_n_ais.getline(buffer, String::CINLIM);and herea = buffer;(operator>>constructor call) and herelen = strlen(s) + 1;(constructor), and it lies in the fact that you get a "buffer" in getline, i.e. not ASCIIZ, but by making the assignment, call the methods of working with ASCIIZ. It may help to putbuffer[is.getline(buffer, String::CINLIM)]=0, but for len = 80 you catch a glitch, then you need to put it in new +1. - nick_n_a