Happy new year it was, all !!! All Cool continuation holidays, meetings with friends, and more happiness to you!
The program causes the debugger. Can tuplyu can not not understand some mystery! Can not!
This, I was not the first day I can not understand that in my code! Like and works almost as expected, but program compiled in Dev-C ++
clicks debugger, on my machine this particular Ida writes the console is fine, but ends with code -3887352130 ......
I have watched a hundred times ... and code nychoho this! Nychoho all I see, are such that could sprychinyty!
I therefore did not understand anything .. I tried to establish with kodeblok- I kodeblok not speak very skillfully, can therefore not understood as nychoho.
It seems something ugly happens when the code reaches line 31 knot = headline1;
Accordingly called into action
Code:
StringBad& StringBad::operator=(const StringBad& st){
if (this==&st)return *this;
len=st.len;
delete [] str;
str=new char(len+1);
std::strcpy(str, st.str); // initialize pointer вгнати значення
std::cout << "StringBad& StringBad::operator=(const StringBad& st) this "<< this << " *this "<< *this << " st " << st << " &len "<< &len << std::endl;
return *this;
}
Just something mysterious happens kind of mysticism, I do not understand, can cause some simple, but I do not understand here.
I know that I can mix anything and anywhere!
Below is the complete code as it is now and coherent message to the project as it was a couple of days ago (all the same)
Code:
// strngbad.cpp -- StringBad class methods
#include <cstring> // string.h for some
#include "strngbad.h"
using std::cout;
// initializing static class member
int StringBad::num_strings = 0; // ініціалізація статичного класового члена
// class methods
// construct StringBad from C string
StringBad::StringBad(const char * s)
{
len = std::strlen(s); // set size встановити розмір
str = new char[len + 1]; // allot storage виділити пам'ять
std::strcpy(str, s); // initialize pointer вгнати значення ні ініціалізувати покажчік
num_strings++; // set object count
cout << num_strings << ": \"" << str
<< "\" object created\n"; // For Your Information
std::cout << "StringBad::StringBad(const char * s) this "<< this << " s " << s << std::endl;
}
StringBad::StringBad() // default constructor
{
len = 0;
str = new char[1];
str [0]='\0'; // default string
num_strings++;
cout << num_strings << ": \"" << str
<< "\" default object created\n"; // FYI
std::cout << "StringBad::StringBad() this "<< this << std::endl;
}
StringBad::StringBad(StringBad& st)
{
len =st.len; // set size встановити розмір
str = new char[len + 1]; // allot storage виділити пам'ять
std::strcpy(str, st.str); // initialize pointer вгнати значення ні ініціалізувати покажчік
num_strings++; // set object count
cout << num_strings << ": \"" << str
<< "\" object created\n"; // For Your Information
std::cout << "StringBad::StringBad(StringBad& st) this "<< this << " st " << st << std::endl;
};
StringBad::~StringBad() // necessary destructor
{
cout << "\"" << str << "\" object deleted, "; // FYI
--num_strings; // required
cout << num_strings << " left\n"; // FYI
delete [] str; // required
std::cout << "StringBad::~StringBad() this "<< this << std::endl;
}
std::ostream & operator<<(std::ostream & os, const StringBad & st)
{
os << st.str;
return os;
}
StringBad& StringBad::operator=(const StringBad& st)
{
if (this==&st)return *this;
len=st.len;
delete [] str;
str=new char(len+1);
std::strcpy(str, st.str); // initialize pointer вгнати значення
std::cout << "StringBad& StringBad::operator=(const StringBad& st) this "<< this << " *this "<< *this << " st " << st << " &len "<< &len << std::endl;
return *this;
}
bool StringBad::operator<( const StringBad &st){
if (std::strcmp(str, st.str)>0)
return true;
else
return false;
}
bool StringBad::operator>(const StringBad &st){
return str< st.str;
}
bool StringBad::operator==(const StringBad &st){
std::cout << "bool StringBad::operator==(const StringBad &st) this "<< this << " *this "<< *this << " st " << st << " &len "<< &len << std::endl;
return (std::strcmp(str, st.str)==0);
}
char & StringBad::operator[](int i)
{
std::cout << "char & StringBad::operator[](int i) this "<< this << " *this "<< *this << std::endl;
return str[i];
}
Code:
// strngbad.h -- flawed string class definition
#include <iostream>
#ifndef STRNGBAD_H_
#define STRNGBAD_H_
class StringBad
{
private:
char * str; // pointer to string покажчік кудись
int len; // length of string длина чогось
static int num_strings; // number of objects число об'єктів
public:
StringBad(const char * s); // constructor
StringBad(); // default constructor
StringBad(StringBad& st);
~StringBad(); // destructor
// friend function
friend std::ostream & operator<<(std::ostream & os,
const StringBad & st);
StringBad & operator=(const StringBad & st);
bool operator<( const StringBad &st);
bool operator>( const StringBad &st);
bool operator==( const StringBad &st);
char& operator[](int i);
};
#endif
Code:
// vegnews.cpp -- using new and delete with classes
// compile with strngbad.cpp
#include <cstdlib>
#include <iostream>
using std::cout;
#include "strngbad.h"
void callme1(StringBad &); // pass by reference
void callme2(StringBad); // pass by value
int main()
{
using std::endl;
StringBad headline1("Celery Stalks at Midnight");
StringBad headline2("Lettuce Prey");
StringBad sports("Spinach Leaves Bowl for Dollars");
cout << "headline1: " << headline1 << endl;
cout << "headline2: " << headline2 << endl;
cout << "sports: " << sports << endl;
callme1(headline1);
cout << "headline1: " << headline1 << endl;
callme2(headline2);
cout << "headline2: " << headline2 << endl;
cout << "Initialize one object to another:\n";
StringBad sailor = sports;
cout << "sailor: " << sailor << endl;
cout << "Assign one object to another:\n";
StringBad knot;
knot = headline1;
cout << "knot: " << knot << endl;
StringBad Test("Test");
StringBad AnsverF("Lov");
//if ("Lov"==AnsverF){
// cout << "Tak " << endl;
//}
StringBad Mu("QWERTYUIOP");
cout << Mu[4] << endl;
cout << "End of main()\n";
system("pause");
return 0;
}
void callme1(StringBad & rsb)
{
cout << "String passed by reference:\n";
cout << " \"" << rsb << "\"\n";
}
void callme2(StringBad sb)
{
cout << "String passed by value:\n";
cout << " \"" << sb << "\"\n";
}
Скачать strngbad2.zip
або
DepositFiles