Novice looking for help

I am working with a piece of code that contains - in part - the following lines.

LIS3DHI2C::~LIS3DHI2C() {

}

Can someone help me to decipher this line?

The curly braces contain nothing but spaces between them, so that tells me they are redundant and can be delted without changing the meaning. So why are they there?

The tilde “~” is a bitwise inversion operator, and its argument is a function. What does a bitwise inversion of a function produce/

I know these are probably stupid questions, but I’m stymied and would be grateful for anyone who can sare the time to explain it to me.

@fguptill, that line defines the object destructor as opposed to the constructor. The constructor is used to create the object to begin with and would look like this:

LIS3DHI2C::LIS3DHI2C() {
// code to execute during object construction goes here
}

The destructor is similar in that it includes the tilde to define it as such. Destructors are defined where it might be important to release resources like memory when the object is no longer needed. Often in small systems like Particle’s, objects are created but not often destroyed. Note that when you define a class, you MUST define a constructor but defining a destructor is optional. :smile:

3 Likes

Thanks peekay123, that’s a big help. Since that one was so easy for you, may I try another one?

What does this code do?

LIS3DHConfig::LIS3DHConfig() {
}

It looks to me to be a function that does nothing. Is it a declaration statement? If so, where is the type? And if it is a declaration, why do you need the null curly braces?

It is the opposite of a destructor... the Constructor

Classes require some study.

Within a class declaration, including the curly braces eliminates the need of defining the constructor elsewhere. The are not "null curly braces" they are simply empty. Remember how you create a function, those curly braces are required in its implementation:

function prototype:

int myFunction(int myArg);

implementation of that function:

int myFunction(int myArg)
{
  // perhaps some code here...
}

in C++ there are many symbols that do more than one special thing. But don't worry, the more confusing they make the language, the more accomplished you feel having learned it.

3 Likes