I've got a problem. When i'm starting my program, it works great, but when it proceeds it says that there is a breakpoint error. Then it says that there is a problem with a memory allocation so it's not working. Here is the code: Header:
#pragma once
#include <iostream>
#include <string>
class basics
{
private:
char *str;
int len;
static int string_obj;
public:
basics() { str = "ces", len = strlen(str), string_obj = 1; }
basics(const basics &st);
basics(const char *s);
~basics();
friend std::ostream & operator<<(std::ostream &os, const basics &st);
};
Class functions:
#include "basics.h"
int basics::string_obj = 0;
basics::basics(const char *s)
{
string_obj++;
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
std::cout << string_obj << " objects was created." << std::endl;
}
basics::basics(const basics & st)
{
string_obj++;
len = std::strlen(st.str);
str = new char[len + 1];
std::strcpy(str, st.str);
std::cout << string_obj << " objects was created." << std::endl;
}
basics::~basics()
{
std::cout << string_obj << " objects was created. Now we'll delete one of them." << std::endl;
--string_obj;
std::cout << string_obj << " objects left." << std::endl;
delete[] str;
}
std::ostream & operator<<(std::ostream &os, const basics &st)
{
os << st.str;
return os;
}
Calling part:
#include "basics.h"
int main()
{
basics gigas("fear");
basics cop(gigas);
gigas.~basics();
system("pause");
return 0;
}
std::string.