Saturday, March 18, 2017

Some C/C++ tips & tricks

Understanding Virtual Functions in C++

Virtual functions are a cornerstone of C++ polymorphism, enabling dynamic binding at runtime. This blog post delves into various aspects of virtual functions with practical code examples and detailed explanations.

Virtual Function Example

Let's start with a basic example that demonstrates the use of virtual functions.

[code]

#include<iostream>
using namespace std;

class Base{
public:
    int a;
        virtual void sum() {
        cout << "sum of class Base" << endl;
    }
};

class Derived : public Base {
public:
    int b;
    void sum() {
         cout << "sum of class Derived" << endl;
    }

};

int main() {
    Base *aptr;
    Derived d;
    aptr = &d;
    aptr->sum();
    return 0;
}


Output:


sum of class Derived

Without the virtual keyword, the output would be "Sum of class Base."


sum of class Base


Virtual Friend Function Idiom

This idiom allows friend functions to act as if they were dynamically bound:


[code]

#include<iostream>
using namespace std;

class Base {
    public:
       friend ostream& operator << (ostream& o, const Base& b);
    protected:
       virtual void print(ostream& o) const
       {  cout << "This is Base class print function" << endl;    }
};

/* make sure to put this function into the header file */
inline std::ostream& operator<< (std::ostream& o, const Base& b)
{
      b.print(o); // delegate the work to a polymorphic member function.
      return o;
}

class Derived : public Base {
  protected:
    virtual void print(ostream& o) const
    { cout << "This is Derived class print function" << endl; }
};

int main(void)
{
     Base b;
     cout << b;
     Derived d;
     cout << d;
     return 0;
}
Output:

This is Base class print function
This is Derived class print function

The end result is that operator<< acts as if it were dynamically bound, even though it's a friend function.
This is called the Virtual Friend Function Idiom. Note that derived classes override printOn(std::ostream&) const. In particular, they do not provide their own operator<<.

Confusing Base/Derived class pointer conversion

This example demonstrates dangerous conversions between base and derived class pointers:
#include<iostream>
using namespace std;

class BB{
public:
         virtual void Doit() { cout << "BB do it" << endl; }
         void Seeit() { cout << "BB see it" << endl; }
         virtual ~BB(){}
};

class DD: public BB{
public:
         virtual void Doit() { cout << "DD do it" << endl; }
         void Seeit() { cout << "DD see it" << endl; }
};

class XX: public BB{
public:
         void Seeit() { cout << "XX see it" << endl; }
         virtual void Doit() { cout << "XX do it" << endl; }
};

int main(void)
{
    DD *dp = new DD;

    BB *bp = static_cast<BB*>(dp);
    bp->Doit();
    bp->Seeit();

    XX *xp = static_cast<XX *>(bp);   //dangerous, please don't do it
    xp->Doit();
    xp->Seeit();

    return 0;
}


Output:
DD do it
BB see it
DD do it
XX see it


However, if we change the main function as follows (by using 'dynamic_cast' instead):

int main(void)
{
    DD *dp = new DD;

    BB *bp = dynamic_cast<BB*>(dp);
    bp->Doit();
    bp->Seeit();

    XX *xp = dynamic_cast<XX *>(bp);
    if (xp == NULL) {
        cout << "Wrong, this should not be allowed" << endl;
    } else {
        xp->Doit();
        xp->Seeit();
    }
    return 0;
}



Constructor and Destructor Calls in Inheritance

When a derived class instance is created, the base class constructor is called first, followed by the derived class constructor. On destruction, the process is reversed.

For example:

[code]   

#include<iostream>
using namespace std;
class Other{
    public:
       int ov;
       Other(int t):ov(t) { cout << "Other constructor" << endl; }
       ~Other(void) { cout << "Other destructor " << endl; }
};
class Base{
public:
    int bv;
    Base(int var):bv(var)
    {
        cout<<"Base constructor"<<endl;
    }
    virtual ~Base(void)
    {
        cout<<"Base destructor"<<endl;
    }
};
class Derived: public Base
{
public:
    int dv;
    Other mt;
    Derived(int d): mt(d++), Base (d++)     // Base class constructor gets call first, then followed by member initialization
    {
        cout<<"child constructor"<<endl;
    }
    ~Derived(void) {
        cout << "child destructor" << endl;
    }
};
int main()
{
    Derived obj1(8);
}

 

The expect result is:

Base constructor
Other constructor
child constructor
child destructor
Other destructor 
Base destructor


Private Virtual Function Usage

Even private virtual functions can be used to enforce derived class behavior without being directly called by the derived class.


[code]

#include<iostream>
using namespace std;

class A{
    public:
       void foo() {
          cout << __PRETTY_FUNCTION__ << endl;
          bar();
       }
    private:
       virtual void bar() {
          cout << __PRETTY_FUNCTION__ << endl;
       }
};

class B: public A {
   private:
       virtual void bar() {
          cout << __PRETTY_FUNCTION__ << endl;
       }
};

int main(void)
{
    A *pa = new B;
    pa->foo();
    return 0;
}
 

Base class requires its Derived class to override its virtual function bar(),  but  it is better for its derived class not call it directly. so the expected result is:

void A::foo()
virtual void B::bar()


Polymorphism in Constructors and Destructors

Virtual functions cannot exhibit polymorphism in constructors and destructors.


#include<iostream>
using namespace std;

class A{
    public:
       A() {
          cout << __PRETTY_FUNCTION__ << endl;
          bar();
       }
       ~A() {
          cout << __PRETTY_FUNCTION__ << endl;
          bar();
       }
       virtual void bar() {
          cout << __PRETTY_FUNCTION__ << endl;
       }
};

class B: public A {
   public:
       virtual void bar() {
          cout << __PRETTY_FUNCTION__ << endl;
       }
};

int main(void)
{
    A *pa = new B;
    delete pa;
    return 0;
}

I would expect the result should be:
<=====Wrong result =====>
A::A()
virtual void B::bar()
A::~A()
virtual void B::bar()
<=====End Wrong result =====>

but the actual result is:

A::A()
virtual void A::bar()
A::~A()
virtual void A::bar()

Pure virtual function

Polymorphism won't be available if a member function is not virtual in the base class. Let's consider the following example:

[code]

#include<iostream>
using namespace std;

class Abase
{
public:
    virtual void FA()= 0;
    void FB()   // notice that there is no virtual here
    {
        cout << __PRETTY_FUNCTION__ << endl;
    }
    virtual void FC()
    {
       cout << __PRETTY_FUNCTION__ << endl;
    }
};

//sub class
class Subase: public Abase
{
public:
    void FA()
    {
       cout << __PRETTY_FUNCTION__ << endl;
    }
    void FB()
    {
       cout << __PRETTY_FUNCTION__ << endl;
    }
    void FC()
    {
      cout << __PRETTY_FUNCTION__ << endl;
    }
};

int main()
{
    Abase* inst = new Subase();
    inst->FA();
    inst->FB();
    inst->FC();
    return 0;

}



The result is:
virtual void Subase::FA()
void Abase::FB()
virtual void Subase::FC()

How many instances get created and which constructor is invoked

Please do some experiment in the following class: 

[code]
#include<iostream>
#include<string>

using namespace std;
#define TRACEPR cout << __PRETTY_FUNCTION__ << endl;

class B{
    private:
       int m = 0;
    public:
       B(int b=0):m(b){
           TRACEPR
       };
       B(const B &b):m(b.m) {
           TRACEPR
       };
       B &operator=(const B &b) {
          TRACEPR
          m = b.m;
          return *this;
       }
       virtual ~B(void) {
           TRACEPR
       }
       B(const B &&b):m(b.m) {
           TRACEPR
       }
       B &operator=(B &&b) {
          TRACEPR
          m = b.m;
          b.m = 0;
          return *this;
       }
};

B getB()
{
     B b;
     return b;
}

B &getrB()
{
     static B b;
     return b;
}

void setB(B b)
{
     B t = b;
}

void setrB( B &b)
{
     B t = b;
}


The compiler specific options as follows:

g++  -g -std=c++11 -fno-elide-constructors  runfile.cpp  -o runfile



Please inspect the following invoke combinations (some have compiler error) , pay attention about what constructor is called and how many instances are created, including xvalue, rvalue and lvalue type. 

1) B b = getB();
2) const B &b = getB(); // what if we don't use const?
3) B b = getrB(); 
4) B &b = getB();
5) setB(b);
6) setrB(b);




Object Slicing  

Object slicing occurs when a derived class object is assigned to a base class object, causing the derived part to be "sliced off."



#include<iostream>
using namespace std;


class B{
    public:
       virtual void F() { cout << "B F() "  << endl; }
       void C() { cout << "B C() " << endl; }
};


class D: public B{
    public:
       void F() { cout << "D F() "  << endl; }
       void C() { cout << "D C() " << endl; }
};


void objnotslicing(B &b)
{
     b.F();
     b.C();
}

void objslicing( B b )
{
     b.F();
     b.C();
}

int main(void)
{
     D d;
     objnotslicing(d);
     objslicing(d);
     return 0;
}



The result is:

// object not slicing happens
D F()
B C()     // please note that C() is a virtual function, hence the polymorphism.

// object slicing happens
B F()
B C()


Understanding and utilizing virtual functions correctly can significantly enhance the flexibility and functionality of your C++ programs. From dynamic binding to preventing object slicing, virtual functions are indispensable tools in advanced C++ programming. Experiment with these examples to deepen your understanding and see the power of virtual functions in action.








No comments:

Post a Comment