assignment & copy constructor
Nov 1, 2016 at 3:12pm UTC
I've created a class called StringBad based on chapter 11 of C++ Primer.
The purpose of the class is to experiment with the copy and assignment constructors:
I get a run time error whenever the following 2 lines of code are uncommented.
1 2
// StringBad sailor = sports;
// cout << "sailor: " << sailor << endl;
main is the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include <iostream>
using std::cout;
using std::endl;
#include "stringbad.h"
void callme1(StringBad &);
void callme2(StringBad);
int main() {
{
cout << "Starting an inner block.\n" ;
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 << "Initialise 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;
cout << "Exiting the block.\n" ;
}
return 0;
}
void callme1(StringBad & rsb) {
cout << "String passed by reference: " ;
cout << " \"" << rsb << "\"\n" ;
}
void callme2(StringBad sb) {
cout << "String passed by value: " ;
cout << " \"" << sb << "\"\n" ;
}
the class definition is :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <iostream>
#include <cstring>
#include "stringbad.h"
int StringBad::num_strings = 0;
StringBad::StringBad(const char * s) {
len = std::strlen(s);
str = new char [len + 1];
std::strcpy(str, s);
num_strings++;
std::cout << "\n" << num_strings << ": \"" << str << "\" object created\n" ;
}
StringBad::StringBad() {
len = 4;
str = new char [4];
std::strcpy(str, "C++" );
num_strings++;
std::cout << "\n" << num_strings << ": \"" << str << "\" default object created\n" ;
}
StringBad::~StringBad() {
std::cout << "\n" << "\"" << str << "\" << object deleted, " ;
--num_strings;
std::cout << "\n" << num_strings << " left\n" ;
delete [] str;
}
StringBad::StringBad(const StringBad & s) {
num_strings++;
str = new char [len + 1];
std::strcpy(str, s.str);
len = s.len;
}
std::ostream & operator <<(std::ostream & os, const StringBad & st) {
os << st.str;
return os;
}
StringBad & StringBad::operator =(const StringBad & st) {
if (this == &st) // in case assigment occurs to self
return *this ;
delete [] str;
len = st.len;
str = new char [len + 1];
std::strcpy(str, st.str);
return *this ;
}
class declaration is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#ifndef STRBAD_H_
#define STRBAD_H_
#include <iostream>
// #include <string>
class StringBad {
private :
char * str;
int len = 0;
static int num_strings;
public :
StringBad(const char *);
StringBad(const StringBad & s);
StringBad();
~StringBad();
StringBad & operator =(const StringBad & st);
friend std::ostream & operator <<(std::ostream & os, const StringBad & st);
};
#endif
Could anybody suggest why the runtime error? I use g++ 5.4 on ubuntu.
Nov 1, 2016 at 3:35pm UTC
stringbad.cpp line 32: len is 0. You want s.len (or move line 34 before line 32).
Topic archived. No new replies allowed.