virtual destructors

closed account (zwA4jE8b)
Can anyone tell me the proper way to implement a virtual destructor?
any maybe even why it is the way it is.
I read that the derived objects destructor will automatically call the base objects destructor when invoked. i.e. delete

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
51
52
53
54
55
56
57
58
class obj_holder
{
	protected:
		float x, y;
		float x_vel, y_vel;
		float tem_grav;
		float air_time;
		bool moving;
	public:
		virtual void handle_input();
		virtual void move();
		virtual void show();
		virtual ~obj_holder();
};

//tried with and without this
obj_holder::~obj_holder(){};

//The man
class Man: public obj_holder
{
    public:
		Man();
		void handle_input();
		void move();
		void show();
		~Man();
};

//The ball
class Ball: public obj_holder
{
    private:
		float t_vel;

    public:
		Ball();
		void handle_input();
		void direction(float, float);
		void move();
		void show();
		~Ball();
};

.....
.....
.....

Man::~Man(){}
Ball::~Ball(){}

Errors using empty implementation
[output]src\main.o:main.cpp:(.text+0x96): undefined reference to `vtable for obj_holder'
src\main.o:main.cpp:(.text+0xa2): undefined reference to `vtable for obj_holder'
src\main.o:main.cpp:(.text+0xae): undefined reference to `vtable for obj_holder'
src\main.o:main.cpp:(.text+0xbe): undefined reference to `vtable for obj_holder'
src\main.o:main.cpp:(.text+0x302): undefined reference to `vtable for obj_holder'
src\main.o:main.cpp:(.text+0x30e): more undefined references to `vtable for obj_holder' follow[/output]


errors not using empty implementation

src\main.o:main.cpp:(.text+0xb2): undefined reference to `obj_holder::~obj_holder()'
src\main.o:main.cpp:(.text+0xe6): undefined reference to `obj_holder::~obj_holder()'
src\main.o:main.cpp:(.text+0x9b): undefined reference to `obj_holder::~obj_holder()'
src\main.o:main.cpp:(.text+0xcf): undefined reference to `obj_holder::~obj_holder()'


1
2
3
4
src\main.o:main.cpp:(.text$_ZN3ManD1Ev[Man::~Man()]+0x6): undefined reference to `vtable for obj_holder'
src\main.o:main.cpp:(.text$_ZN4BallD1Ev[Ball::~Ball()]+0x6): undefined reference to `vtable for obj_holder'
src\main.o:main.cpp:(.text$_ZN4BallD0Ev[Ball::~Ball()]+0x6): undefined reference to `vtable for obj_holder'
src\main.o:main.cpp:(.text$_ZN3ManD0Ev[Man::~Man()]+0x6): undefined reference to `vtable for obj_holder'
Last edited on
Implement a definition of the virtual functions in obj_holder or make them pure virtual.
Last edited on
closed account (zwA4jE8b)
Thanks Peter87

This works!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class obj_holder
{
	protected:
		float x, y;
		float x_vel, y_vel;
		float tem_grav;
		float air_time;
		bool moving;
	public:
		virtual void handle_input(){};
		virtual void move(){};
		virtual void show(){};
		virtual ~obj_holder(){};
};




This does not work!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class obj_holder
{
	protected:
		float x, y;
		float x_vel, y_vel;
		float tem_grav;
		float air_time;
		bool moving;
	public:
		virtual void handle_input() = 0;
		virtual void move() = 0;
		virtual void show() = 0;
		virtual ~obj_holder() = 0;
};



src\main.o:main.cpp:(.text+0xb2): undefined reference to `obj_holder::~obj_holder()'
src\main.o:main.cpp:(.text+0xe6): undefined reference to `obj_holder::~obj_holder()'
src\main.o:main.cpp:(.text+0x9b): undefined reference to `obj_holder::~obj_holder()'
src\main.o:main.cpp:(.text+0xcf): undefined reference to `obj_holder::~obj_holder()'


do you happen to know why?
Last edited on
If you make the destructor pure virtual you still need to define the destructor.
closed account (zwA4jE8b)
Cool, thank you.

This works too!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class obj_holder
{
	protected:
		float x, y;
		float x_vel, y_vel;
		float tem_grav;
		float air_time;
		bool moving;
	public:
		virtual void handle_input()=0;
		virtual void move()=0;
		virtual void show()=0;
		virtual ~obj_holder() = 0;
};

obj_holder::~obj_holder(){std::cout << "~obj_holder ";}
closed account (zwA4jE8b)
one last question...

If I leave the implementation blank it still destructs properly, correct?
The memory is deallocated when I call delete?

or do I have to write delete this; in the destructor?
Last edited on
Hi ,
There is no need to make the virtual destructor as pure .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class obj_holder
{
	protected:
		float x, y;
		float x_vel, y_vel;
		float tem_grav;
		float air_time;
		bool moving;
	public:
		virtual void handle_input() = 0;
		virtual void move() = 0;
		virtual void show() = 0;
		virtual ~obj_holder() {}     //changed over here . 
};


cheers
closed account (zwA4jE8b)
cool, both ways work. Is there any difference? besides one extra line of code?
there is no need to write this

Cheers :)
closed account (zwA4jE8b)
sweet, thanks guys!
Topic archived. No new replies allowed.