2

How can I add my own method to a pre-existing class without any change in context of pre-existing class.

for example :

   A.hpp
        class A
        {
        public :
        void print1()
        {
          cout << "print1";
        }
        };

    B.hpp
        //add a helper function to class A
        //for example:
        A::print2()
        {
        cout << "print2";
        }

     main.cpp

        #include "A.hpp"
        #include "B.hpp"
        main()
        {
           A a1;
           a1.print2();
        }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Mostafa Sataki
  • 305
  • 3
  • 11
  • [Inheritance](http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_programming%29)? [Composition](http://en.wikipedia.org/wiki/Object_composition)? – Some programmer dude Jul 14 '13 at 09:59

1 Answers1

5

To extend a class in C++, distinguish two cases.

If the new function can be expressed in terms of the current interface, use non-member functions

// B.hpp
void print2(A const& a)
{
    // pre-call extensions (logging, checking etc.)
    a.print1();
    // post-call extensions (logging, checking etc.)
}

If the new function requires knowledge about the current implementation, use class inheritance

// B.hpp
// WARNING: here be dragons, read on before using this in production code
class B: public A
{
public:
    void print2() const // compiler-generated signature: void print2(B const*)
    {
        // pre-call extensions (logging, checking etc.)
        print1();
        // post-call extensions (logging, checking etc.)
    }
};

However, deriving from a class that was not intended to be a base class, can be dangerous. In particular, if A does not have a virtual destructor, you can get into trouble if you ever use pointers to dynamically allocated B objects in places where they would be deallocated as if they were pointers to A objects.

Furthermore, because A::print1() was not made virtual, you get into all sorts of name hiding problems, which is why you have to name the extended function B::print2().

Long story short: know which kind of classes you are writing. If you want to extend the behavior based on a class implementation, then you better make it suitable as a base class (virtual destructor, virtual functions that you can override). Otherwise, mark your class as final (new C++11 contextual keyword). This will generate compiler warnings if you try to override existing functions.

NOTE: In other languages (notably D), it is possible to let the compiler automatically find non-member functions print2(a) when it sees the syntax a.print2(). Unfortunately, such uniform function call syntax is not on the roadmap yet for C++.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • 1
    In the delphi compiler when we need to increase the ability of class( do not have the source) we can write a helper function for class.this helper function is similar to static function and to public properties has access. – Mostafa Sataki Jul 20 '13 at 19:52
  • 1
    This answer is a very long form of the word "no"... but +1. – einpoklum Dec 05 '15 at 22:15