destructor is at war with cascading of stream insertion operator

I have this code:

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
#include <iostream>
#include <string.h>
#include <iomanip>

using namespace std;

class test{
	
	private:
		
		int i;
		char * c;
		
	public:
		
		test();
		
//		~test();
		
		friend ostream & operator << (ostream &, const test);
	
};

test::test(){
	
	i = 5;
	strcpy(c, "c-constructor-copy");
	
}//------constructor

//test::~test(){}//---------destructor

ostream & operator << (ostream & so, const test tobj){
	
	so << setw(10) << setfill('^') << tobj.i << endl;
	so << tobj.c << endl;
	
	return so;
	
}//---------operator <<

int main(){
	
	test tobj;
	
	cout << tobj << tobj << "\nwhat is happening?";
	
}//--------main() 


And it works fine Unless

1. I uncomment lines 18 and 31. In other words I put a destructor in class.

OR

2. After using destructor I don't repeat the use of cout with test class

If any of these two conditions is met then code compiles successfully but crashes when runs
Last edited on
closed account (z05DSL3A)
char * c;

strcpy(c, "c-constructor-copy");

No storage allocated for your copy = a trashed stack.
Oh Wolf how thankful I am to you!!! thankyou very much.
Topic archived. No new replies allowed.