sshekhar1980
sshekhar1980

Reputation: 367

C++ Function with empty body vs function with no body

I am trying to understand the difference between having no body and having an empty body, as in these two functions:

void draw() const override; 
void rotate(int angle) override {}

The complete code is shown below, where Shape is an interface and Circle an implementation of that interface.

#include<iostream>
#include<vector>
using namespace std;

class Point {
    public:
        double x() {
            return x_coordinate;
        }
        double y() {
            return y_coordinate;
        }
    private:
        double x_coordinate, y_coordinate;
};

class Shape {
    public:
        virtual Point center() const =0;
        virtual void move(Point to) =0;
        virtual void draw() const =0;
        virtual void rotate(int angle) =0;
        virtual ~Shape() {}
};

class Circle: public Shape {
    public:
        Circle(Point p, int radius);
        Point center() {
            return c;
        }
        void move(Point to) {
            c = to;
        }
        void draw() const override; 
        void rotate(int angle) override {}
    private:
        Point c;    
        int r;
};

Upvotes: 2

Views: 1629

Answers (3)

pmw1234
pmw1234

Reputation: 218

A function declaration tells the compiler everything it needs to know to call a function. With that information the compiler can generate intermediate files such as "obj" files.

Once the code is compiled, the next step is to link it, during linking the linker has to put the actual location for the code into the final exe. This where the function definition comes in, if the linker can't find the function definition you will get an error.

You can seperate the function declaration from the definition which is common practice as it helps speed up compiling, makes files clear and easy to read, and a few other niceties.

So:

 void draw() const override; 

Is a declaration, telling the compiler what it needs to call it, but there is not enough information to actually jump to the code (since there is no code at all).

void rotate(int angle) override {}

Is a both, a declaration and a definition. This gives the compiler enough info to call it, and the linker enough info to jump to the actual code. The "{}" has a little bit of implied code and would expand to something like:

void rotate(int angle) override {return;} // do nothing and return

With classes, things get a bit murkier, but these basic rules always apply to every function, whether they are templates, classes, or just plain old c style code.

Upvotes: 3

Sol Arnu
Sol Arnu

Reputation: 317

It looks like the first one draw() is just a prototype while the second one rotate() is a no-op (i.e., does nothing.) If you call the first function, it will not link (edit!), while a call to the second one will compile and link but just will not do anything.

Upvotes: 1

Adrian Mole
Adrian Mole

Reputation: 51845

The void draw() const override; line is not a function definition at all, merely a declaration; the compiler (or, possibly, the linker) expects there to be an actual definition, including a (possibly empty) body, somewhere else in your code.

The void rotate(int angle) override {} line is a complete definition – of a function that actually does nothing. Once you provide a function 'body', you have provided a definition – even if that body is empty (as is your {}).

Upvotes: 2

Related Questions