How does this template work
-
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.
-
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.
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++ -
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.
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] -
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.
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.
-
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] -
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?
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
-
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?
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++ -
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++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:
-
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:
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++ -
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++ -
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.
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++ -
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++