Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How does this template work

How does this template work

Scheduled Pinned Locked Moved C / C++ / MFC
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A AkashAg

    class A
    {
    int i;
    public:
    void func1() {cout << "func1" << endl;};
    static void func2() {cout << "func2" << endl;};
    void func3() {i =1; cout << "func3" << endl;};
    };

    template <class T> class Smart
    {
    private :
    T *m_ptr;
    public :
    explicit Smart(T *p = 0) : m_ptr(p) {}
    T& operator*() {return *m_ptr;}
    T* operator->() {return m_ptr;}
    ~Smart() {delete m_ptr;}
    };

    int main()
    {
    Smart<A> s(0);
    (*s).func1();

    system("PAUSE");
    }

    On running it I saw that it prints "func1" but how it it possible. I initialize it as NULL.

    C Offline
    C Offline
    Cedric Moonen
    wrote on last edited by
    #3

    Basically your problem is resumed to something like this:

    A* a = NULL;
    a->func1();

    In fact, the code will not crash in such scenario because you do not access any member variable of the class. Class methods are similar to global function except that they take an additional implicit parameter: the 'this' pointer (in your case it will be NULL). So, as long as you don't access any class variable, the code will not crash. Try now to print the value of 'i' in func1 and you'll that the code will crash.

    Cédric Moonen Software developer
    Charting control [v3.0] OpenGL game tutorial in C++

    1 Reply Last reply
    0
    • A AkashAg

      class A
      {
      int i;
      public:
      void func1() {cout << "func1" << endl;};
      static void func2() {cout << "func2" << endl;};
      void func3() {i =1; cout << "func3" << endl;};
      };

      template <class T> class Smart
      {
      private :
      T *m_ptr;
      public :
      explicit Smart(T *p = 0) : m_ptr(p) {}
      T& operator*() {return *m_ptr;}
      T* operator->() {return m_ptr;}
      ~Smart() {delete m_ptr;}
      };

      int main()
      {
      Smart<A> s(0);
      (*s).func1();

      system("PAUSE");
      }

      On running it I saw that it prints "func1" but how it it possible. I initialize it as NULL.

      H Offline
      H Offline
      hanq_38910130
      wrote on last edited by
      #4

      There is difference between "global function" and "class member function" only when the latter refers to the object's "this pointer". If not, these two functions are the same. In your situation, the "(*s).func1();" doesn't refer to "this pointer". So it behaves just like a "global function". It doesn't use "this pointer" which is NULL here, hence no error there.

      1 Reply Last reply
      0
      • CPalliniC CPallini

        Weird as it might look, a instance of the class is not really needed when calling such method, try the following simple code:

        #include <iostream>
        using namespace std;

        class A
        {
        public:
        void show(){ cout << "The show must go on." << endl; }
        };

        int main()
        {
        A * pA = NULL;
        pA->show();
        }

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        A Offline
        A Offline
        AkashAg
        wrote on last edited by
        #5

        I tried this and it worked find. But I am still confusing why it is working. What are the scenarios in which this thing will work. Does it mean that U can access methods from NULL pointer but not attribute?

        CPalliniC C 2 Replies Last reply
        0
        • A AkashAg

          I tried this and it worked find. But I am still confusing why it is working. What are the scenarios in which this thing will work. Does it mean that U can access methods from NULL pointer but not attribute?

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #6

          akashag wrote:

          Does it mean that U can access methods from NULL pointer but not attribute?

          Yes, try:

          #include <iostream>
          using namespace std;

          class A
          {
          public:
          int a;
          A():a(1){}
          void show(){ cout << "The show must go on." << endl; }
          void dump(){ cout << "member value is << " << a << endl;}
          };

          int main()
          {
          A * pA = NULL;
          pA->show();
          pA->dump();
          }

          [added] From a OOP point of view (IMHO) this is a point where object abstraction clashes with technical implementation. [/added] :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          modified on Wednesday, April 28, 2010 3:36 AM

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • A AkashAg

            I tried this and it worked find. But I am still confusing why it is working. What are the scenarios in which this thing will work. Does it mean that U can access methods from NULL pointer but not attribute?

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #7

            Yes, as I said earlier a class method is similar as a global method (so, there is one single function even if there are mutliple instances of the class or even no instance at all). When the method is called, the address of the instance on which the function is called is passed to the function (the 'this' pointer). This means that as long as you don't access any variables of the class, you can perfectly call the function even if the instance is NULL.

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            E 1 Reply Last reply
            0
            • C Cedric Moonen

              Yes, as I said earlier a class method is similar as a global method (so, there is one single function even if there are mutliple instances of the class or even no instance at all). When the method is called, the address of the instance on which the function is called is passed to the function (the 'this' pointer). This means that as long as you don't access any variables of the class, you can perfectly call the function even if the instance is NULL.

              Cédric Moonen Software developer
              Charting control [v3.0] OpenGL game tutorial in C++

              E Offline
              E Offline
              Emilio Garavaglia
              wrote on last edited by
              #8

              There is one exception: if the function call is indierect because of a v-table (the method is virtual), even if the function itself don't refer the _this_ pointer, a valid _this_ is required to perform the runtime redirection.

              2 bugs found. > recompile ... 65534 bugs found. :doh:

              C 1 Reply Last reply
              0
              • E Emilio Garavaglia

                There is one exception: if the function call is indierect because of a v-table (the method is virtual), even if the function itself don't refer the _this_ pointer, a valid _this_ is required to perform the runtime redirection.

                2 bugs found. > recompile ... 65534 bugs found. :doh:

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #9

                Yes, I forgot that. Mmmh, let's say I didn't want to confuse the OP with too much information ;P

                Cédric Moonen Software developer
                Charting control [v3.0] OpenGL game tutorial in C++

                L 1 Reply Last reply
                0
                • C Cedric Moonen

                  Yes, I forgot that. Mmmh, let's say I didn't want to confuse the OP with too much information ;P

                  Cédric Moonen Software developer
                  Charting control [v3.0] OpenGL game tutorial in C++

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  Cedric Moonen wrote:

                  I didn't want to confuse the OP with too much information

                  But what about me? I am still trying to absorb this. :confused:

                  It's time for a new signature.

                  C 1 Reply Last reply
                  0
                  • L Lost User

                    Cedric Moonen wrote:

                    I didn't want to confuse the OP with too much information

                    But what about me? I am still trying to absorb this. :confused:

                    It's time for a new signature.

                    C Offline
                    C Offline
                    Cedric Moonen
                    wrote on last edited by
                    #11

                    Err, what do you mean exactly ? I don't get it...

                    Cédric Moonen Software developer
                    Charting control [v3.0] OpenGL game tutorial in C++

                    L 1 Reply Last reply
                    0
                    • C Cedric Moonen

                      Err, what do you mean exactly ? I don't get it...

                      Cédric Moonen Software developer
                      Charting control [v3.0] OpenGL game tutorial in C++

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      I mean I was confused by this thread, but now I'm beginning to understand, thanks to your comments.

                      It's time for a new signature.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups