reference parameter in constructor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class engine{
public:
	engine();
	engine(const engine& eng);
	engine(double mass);
	~engine();
	void run();
private:
...
};
class rocket{
public:
	rocket(string name, double mass, double dia, const engine& eng);
	~rocket();
	int run();
private:
...
engine engine1;
...
};

As you can see from the code above, there are two classes. In the second one, the constructor has a
const engine& eng parameter. The code can be compiled, but there is a segmentation fault at runtime.
If const engine& eng is replaced with const engine eng, everything goes well. Why?
(the entire code is too long to be posted here)
Post the engine copy constructor, that rocket constructor, and both destructors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
rocket::rocket(string name, double mass, double dia, const engine eng):
		PI(3.1415926), rocket_name(name), diameter(dia), mass_dry(mass),
		engine1(eng), drag_coefficient(0.75), grav_const(9.8),
		area((diameter/2)*(diameter/2)*PI)
{

	velocity = 0;
	altitude = 0;
	time = 0;
	interval->tv_sec = 0.2;
	interval->tv_nsec = 0;
	G = 0;
	F = 0;
	drag_force = 0;

}

engine::engine(const engine& eng):pro_mass(eng.pro_mass), exit_area(eng.exit_area),
		interval(eng.interval), type_name(eng.type_name), casing_mass(eng.casing_mass),
		mass_decrement(eng.mass_decrement), thrust(0), impulse(0), burn_time(0),
		press(20000)
{
}

both constructors do nothing.
here is my main:
1
2
3
4
5
6
7
8
9
int main()
{

	engine eng1(1200);
	rocket rocket1("Rocket", 200, 1, eng1);
	rocket1.run();

	return 0;
}
Is engine::interval dynamically allocated? What about rocket::interval?
I got it!

rocket::interval is a struct timespec* pointer. I now changed it to

struct timespec interval; It works.

Also, I changed the declearation and initialization order of the private data members (first const members, then others) to make the compiler not output so many warnings.
Thank you very much!!! Your post helped me a lot.
Topic archived. No new replies allowed.