Virtual destrucktor in Adapter patterns

Code1 CTaxi.h
1
2
3
4
5
6
7
8
9
10
11
12
#include<iostream>
class CTaxi
 {
    public:
        CTaxi();
        ~CTaxi();
        void reinigen();
        double taxameter();
    private:
        double m_taxameter;
 };

Code2 CAuto.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
class CAuto
 {
    public:
        CAuto();
        CAuto(std::string farbe);
        virtual ~CAuto();
        virtual std::string getFarbe();
        virtual void lackiere(std::string farbe);
        virtual void waschen() = 0;
    private:
        std::string m_farbe;
 };

Code3 CTaxiAdapter.h
1
2
3
4
5
6
7
8
9
10
11
12
#include"CAuto.h"
#include"ctaxi.h"
class CTaxiAdapter :
 public CAuto,
 private CTaxi
 {
    public:
        CTaxiAdapter();
        ~CTaxiAdapter();
        void waschen();
 };

Code4 CTaxiAdapter.cpp
1
2
3
4
5
6
7
#include"ctaxiadapter.h"
CTaxiAdapter::CTaxiAdapter():CAuto("taxigelb"){}

CTaxiAdapter::~CTaxiAdapter(){}
void CTaxiAdapter::waschen()
 { CTaxi::reinigen();
 }

Code5 CAuto.cpp
1
2
3
4
5
6
7
8
9
10
#include<iostream>
#include"CAuto.h"
CAuto::CAuto()
{}
CAuto::CAuto(std::string farbe)
{
    m_farbe=farbe;
    }


Code6 CTaxi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include"ctaxi.h"

CTaxi::CTaxi()
{std::cout<<"CTaxi::CTaxi()"<<std::endl;}
CTaxi::~CTaxi()
{std::cout<<"CTaxi::  ~CTaxi()"<<std::endl;}
void CTaxi::reinigen()
{std::cout<<"void CTaxi::reinigen()"<<std::endl;}
double CTaxi::taxameter()
{std::cout<<"double CTaxi::taxameter()"<<std::endl;
    return 100;}

Code7
1
2
3
4
5
6
7
8
9
10
11
#include"ctaxiadapter.h"

int main(){

    CAuto *taxi = new CTaxiAdapter;
    taxi->waschen();
    std::cout << taxi->getFarbe() << std::endl;
    //cout << taxi->    taxameter() << endl; // Compilerfehler
    delete taxi;
    }


It is a exsample in the book,but was not completed,i write the CAuto.cpp and CTaxi.cpp myself.
But when I try to compiler it,I get this erro Msg
\Adapter\ctaxiAdapter.cpp|2|undefined reference to `CAuto::~CAuto()'|
I guess somting is wrong with the Virtual destrucktor.
How can I slove this problem.
You have declared CAuto::~CAuto() in the header file but you have not provided an implementation for it anywhere.
yes, I know but this is a
virtual ~CAuto();
should I write somthing like
virtual CAuto::~CAuto(){};


just do CAuto::CAuto() {};
for the implementation
Last edited on
I get erro like:
Adapter\CAuto.cpp|10|error: virtual outside class declaration|
Okay then everything is all right,thanks =).
But I have a question,why use virtual in the base class,
and what is the advantage of that?
for example it is useful in polymorphism...

you already know about inheritance and upcasting a dereived class to its bass class?...

if u got an pointer of the base class, that points to a child and then call the destructor - which is not virtual- u won´t reach the dereived class destructors...

(short and as far as i know^^)
thank you.I think I get what you mean.
=)
np...

wenn noch weitere fragen dazu hast... mail an: question@incubbus.eu :P
Topic archived. No new replies allowed.