Saturday, October 13, 2012


In GNU Radio every new signal processing block is derived from the base class gr_block. Whenever we make a class we make a constructor for that class. This constructor has to mostly public but in GNU radio you will find that constructors to the gr_block are private for some special reason. Details can be found at

Lets see what does a constructor do :

What is a constructor?

When an object of a class is created, C++ calls the constructor for that class. If no constructor is defined, C++ invokes a default constructor, which allocates memory for the object, but doesn't initialize it.

Why you should define a constructor

Uninitialized member fields have garbage in them. This creates the possibility of a serious bug (eg, an uninitialized pointer, illegal values, inconsistent values, ...).
When you create an object, if you do not declare a constructor, the compiler would create one for your program; this is useful because it lets all other objects and functions of the program know that this object exists. This compiler created constructor is called the default constructor. If you want to declare your own constructor, simply add a method with the same name as the object in the public section of the object. When you declare an instance of an object, whether you use that object or not, a constructor for the object is created and signals itself.

 Reference :

Declaring a constructor
A constructor is similar to a function, but with the following differences.
  • No return type.
  • No return statement.
**A constructor is declared without a return value, that also excludes void.

There can be multiple constructors and they can be called/differentiated according to the parameters they have been passed.

Following is a very good example to see the multiple constructors :

using namespace std;


Demonstration of additional facilities such as:
* defaulted parameters
* multiple constructors


class Pet {
                Pet(char *breed, char *name = "Anon");
                char * getBreed();
                char * getName();
                char *itsname;
                char *itsbreed;

Pet::Pet() {
        itsname = "undefined";
        itsbreed = "unknown";

Pet::Pet(char *breed, char *name) {
        itsname = name;
        itsbreed = breed;

char * Pet::getBreed() {
        return itsbreed;

char * Pet::getName() {
        return itsname;

int main () {
        int npets=5;
        Pet *menage[npets];

        menage[0] = new Pet("Hamster","Hammie");
        menage[1] = new Pet();
        menage[2] = new Pet("Dog");
        menage[3] = new Pet("Cat","Charlie");
        menage[4] = new Pet();

        for (int k=0; k                cout << "Pet " << k << " is "
                << menage[k]->getBreed() << " called "
                << menage[k]->getName() << endl ;

The output of the program is :

Pet 0 is Hamster called Hammie
Pet 1 is unknown called undefined
Pet 2 is Dog called Anon
Pet 3 is Cat called Charlie
Pet 4 is unknown called undefined


Reference : 

No comments:

Post a Comment