-1

I have this class :

class CNullDecorator: public CBaseDecorator, public CPooledObject<CNullDecorator>
{
public:
    CNullDecorator(){}
    virtual~CNullDecorator(){}

protected:
    virtual void __Excute(const CDecoratorData &d){}
    virtual CBaseDecorator *__Clone(CParticleInstance *, CParticleInstance *)
    {
        return new CNullDecorator;
    }
};

As you can see CNullDecorator(){} and virtual~CNullDecorator(){} have some brackets open but nothing inside brackets. My question is why? Which scope have that brackets?

CinCout
  • 9,486
  • 12
  • 49
  • 67
marc
  • 1
  • 7

3 Answers3

1

They are inline functions that do nothing. You could have

 CNullDecorator () { return ; }

and get the same effect.

user3344003
  • 20,574
  • 3
  • 26
  • 62
0

{ } defines a function body. In your case, this necessarily means that CNullDecorator() (constructor), ~CNullDecorator() (destructor) and __Excute() (protected method) have no body, i.e., they do nothing.

EDIT: (answering the question asked in comments)

If CNullDecorator is not used. Or a constructor or destructor is never used and just defined can i delete it?

Constructor and destructor (along with few other stuff) is what the C++ compiler provides by default in case the programmer omits them. In your case, the constructor can be removed since it does nothing, but the destructor you've made is virtual (although empty), so you will have to keep it to behave the way it is expected to.

More on virtual destructor here.

Community
  • 1
  • 1
CinCout
  • 9,486
  • 12
  • 49
  • 67
  • If CNullDecorator is not used. Or a constructor or destructor is never used and just defined can i delete it? – marc Jun 16 '16 at 03:00
  • It is recommended that you edit your question to include the above query than asking it in comments. – CinCout Jun 16 '16 at 03:05
0

First of all, it's not brackets, but braces.

All scopes have braces. But some scopes are more special than others. A function is an entire scope. In this case:

CNullDecorator(){}

This declares an empty constructor, a constructor with no code. Normally, you'll have a constructor doing something:

CNullDecorator()
{
     // Some code here. Initialize this to that. Call some other
     // function
}

Pretend that there's code inside the braces. Now you have a typical constructor. But nothing actually requires you to have any code in the constructor. So, if you leave it out, you'll have an empty set of braces.

This doesn't really do anything useful. You can leave out the constructor entirely, and the compiler will happily supply it to you. It's a matter of style.

On the other hand, there is a reason why the destructor is the way it is:

virtual ~CNullDecorator(){}

This also declares a destructor that's empty, and contains no code.

But in this case, there's a reason for this. Like with a constructor, if you leave out the destructor completely, the compiler will supply a default destructor.

But this declaration does something a little bit more: it declares a virtual destructor.

In this case, all this class needed is a virtual destructor. The virtual destructor didn't need to do anything explicit, hence its contents were empty. But, the class required a virtual destructor, so it had to be declared.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148