DevC++ recursive constructors

I started to write a text game for university task and realized that i cant create an child object from the constructor. The application just closes without prompting. I've tried to find some examples in the net... this code do the same thing in DevC++

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
# include <iostream.h>
# include <conio.h>
# include <process.h>

class tri_num
{
	int f;
	public :
		tri_num( ) { f=0; }
		void sum( int j) {   f=f+j;  }
		tri_num(int m){
			if (m==0) {
				cout <<"Triangular number : "<<f;
				exit(1);
			};
			sum(m);
			tri_num::tri_num(--m);
		}

};

int main()
{
//tri_num::tri_num(5); //Also doesn't work
tri_num* a= new tri_num(5);
getche();
}


window appears for the moment and dissappears.
Last edited on
Program closes with code 1, when creating fifth generation of children, when it hits line 14. What did you expect to happen?
Last edited on
Well I found that it exits in first recursive call.
This was just a code i found as recursive example.
I didn' posted my code.
I'm working on getting rid of recursivity in my program.
I made an init method that finishes everything constructor should do. Hope that would work.
Last edited on
It does not exit in first recursive call in my debugger. It cannot - since in fist call you pass m=5, and in next recursion it can only be 4, no less, hence not 0.
Last edited on
Topic archived. No new replies allowed.