1

I have this class.h:

class CItemPriceListTableCache: public cache< TItemPriceListTable >
{
public:
    virtual ~CItemPriceListTableCache();    
};

And i have this class.cpp :

CItemPriceListTableCache::~CItemPriceListTableCache()
{

}

Now come questions : Why is ~CItemPriceListTableCache funtion body empty ? If i delete from class.cpp the ~CItemPriceListTableCache should be ok ? Will affect this my codes ? With what should i replace ~CItemPriceListTableCache function body ? I just don't like to see empty functions. Even if i have one line in function is ok for me , i just don't like function to be empty. If i complete delete virtual decalaration of destructor from class should be ok ?

EDIT 1: Removed useless txt from question.

EDIT 2: class.h

class DH2KeyAgreement: public KeyAgreement
{
public:
    DH2KeyAgreement();
};

class.cpp

DH2KeyAgreement::DH2KeyAgreement() : dh_(), dh2_(dh_)
{

}

How should i use here the default ?

Should e fine like this ?

class DH2KeyAgreement: public KeyAgreement
{
public:
    DH2KeyAgreement():dh_(), dh2_(dh_)=default;
};
Smartx221
  • 19
  • 5

2 Answers2

4

Why is ~CItemPriceListTableCache funtion body empty ?

Because no special action needs to be taken on destruction, but the destructor still should be callable for this class.

If i delete from class.cpp the ~CItemPriceListTableCache should be ok ? Will affect this my codes ?

Yes, you will be left with an undefined reference error.

With what should i replace ~CItemPriceListTableCache function body ? I just don't like to see empty functions. Even if i have one line in function is ok for me , i just don't like function to be empty.

You can write

virtual ~CItemPriceListTableCache() {}

or

virtual ~CItemPriceListTableCache() = default;

in your class declaration.

If i complete delete virtual decalaration of destructor from class should be ok ?

Yes, the default destructor generated by the compiler, will be just fine.

πάντα ῥεῖ
  • 83,259
  • 13
  • 96
  • 175
  • 1
    If the base class has a virtual destructor there is no need to write an empty virtual destructor in the derived class. The compiler-generated destructor will be virtual. – Pete Becker Jun 19 '16 at 20:09
  • @πάνταῥεῖ I edited question to avoid another .. Ca you look ? – Smartx221 Jun 26 '16 at 08:50
0

If there is no destructor defined for a class, the default destructor is used. But then what happens is that when a class has for example a constructor declared virtual, some compilers will warn like say Virtual constructor seen, but no virtual destructor and it is important that you always ensure that no warnings are floating around when you compile your code, therefore a programmer might define a virtual destructor and leave it's body empty just to do away with the warnings (In this case virtual destructor does the same job as the default constructor, except that it is declared virtual). It doesn't hurt to keep it in there, still one day you might need it.

Try removing them and recompile to see whether any warnings are shown, if there aren't any warnings then you can remove it all together (but good luck though with your compiler, because a good compiler should have warned you in this case)

Robert Odoch
  • 388
  • 3
  • 7