| View previous topic :: View next topic |
| Author |
Message |
Gurikar Guest
|
Posted: Fri Apr 29, 2005 11:25 am Post subject: Template function |
|
|
Hello,
Can anyone tell me whats wrong in this?
// template function
template<class T>
inline void test(T* ptr)
{
cout<<"Pointer is"<
ptr = NULL;
cout<<"Pointer is"<
}
// Some class
Class A()
{
A();
}
// main function
int main()
{
A* pObj = new A();
test(pObj );
if (pObj == NULL)
{
cout<<"Mem fail"<
}
My question, even after making the pointer pObj = NULL in template
function test, when it comes to main the value of pObj wont be NULL,
Why is this???
Regards
}
|
|
| Back to top |
|
 |
Tim Love Guest
|
Posted: Fri Apr 29, 2005 11:38 am Post subject: Re: Template function |
|
|
"Gurikar" <msgurikar (AT) gmail (DOT) com> writes:
| Quote: | Hello,
Can anyone tell me whats wrong in this?
...
My question, even after making the pointer pObj = NULL in template
function test, when it comes to main the value of pObj wont be NULL,
Why is this???
|
For now forget about templates and look at something like
#include <iostream>
void test(int *ptr)
{
ptr=0;
}
int main() {
int* pObj = new int();
test(pObj );
if (pObj == 0)
std::cout<<"Mem fail"<
}
|
|
| Back to top |
|
 |
John Bufton Guest
|
Posted: Fri Apr 29, 2005 11:45 am Post subject: Re: Template function |
|
|
Gurikar wrote:
| Quote: | Hello,
Can anyone tell me whats wrong in this?
// template function
template
inline void test(T* ptr)
{
cout<<"Pointer is"<
ptr = NULL;
cout<<"Pointer is"<
}
// Some class
Class A()
{
A();
}
// main function
int main()
{
A* pObj = new A();
test(pObj );
if (pObj == NULL)
{
cout<<"Mem fail"<
}
My question, even after making the pointer pObj = NULL in template
function test, when it comes to main the value of pObj wont be NULL,
Why is this???
Regards
}
|
The pointer is passed by value. Hence the pointer value in the calling
function remains unchanged. Try passing by reference if you want the
pointer of the calling function to be affected.
John Bufton
|
|
| Back to top |
|
 |
Gurikar Guest
|
Posted: Fri Apr 29, 2005 11:48 am Post subject: Re: Template function |
|
|
why is this??????
|
|
| Back to top |
|
 |
Gurikar Guest
|
Posted: Fri Apr 29, 2005 11:51 am Post subject: Re: Template function |
|
|
Can i do this:
void test(int*& ptr = NULL)
{
ptr=0;
}
|
|
| Back to top |
|
 |
Sharad Kala Guest
|
Posted: Fri Apr 29, 2005 12:01 pm Post subject: Re: Template function |
|
|
"Gurikar" <msgurikar (AT) gmail (DOT) com> wrote in message
| Quote: | Can i do this:
void test(int*& ptr = NULL)
{
ptr=0;
}
|
Why not try on a compiler ? The answer is no, most good compilers will also
flash the reason.
|
|
| Back to top |
|
 |
John Bufton Guest
|
Posted: Fri Apr 29, 2005 12:02 pm Post subject: Re: Template function |
|
|
Gurikar wrote:
| Quote: | Can i do this:
void test(int*& ptr = NULL)
{
ptr=0;
}
|
No.
Prototype definition can have default parameter value, but not the
function itself. Hence the following is OK.
// Function prototype definition with default parameter value
void test(int*& ptr = NULL);
// Function implementation
void test(int *&ptr)
{
ptr = NULL;
}
John Bufton
|
|
| Back to top |
|
 |
Sharad Kala Guest
|
Posted: Fri Apr 29, 2005 12:02 pm Post subject: Re: Template function |
|
|
"Gurikar" <msgurikar (AT) gmail (DOT) com> wrote in message
Because your problem has nothing to do with templates. btw, it's a good idea
to not snip the context entirely when you reply.
Sharad
|
|
| Back to top |
|
 |
John Bufton Guest
|
Posted: Fri Apr 29, 2005 12:10 pm Post subject: Re: Template function |
|
|
John Bufton wrote:
| Quote: | Gurikar wrote:
Can i do this:
void test(int*& ptr = NULL) { ptr=0;
}
No.
Prototype definition can have default parameter value, but not the
function itself. Hence the following is OK.
// Function prototype definition with default parameter value
void test(int*& ptr = NULL);
// Function implementation
void test(int *&ptr)
{
ptr = NULL;
}
John Bufton
|
Apologies the above is wrong, (sheer carelessness). The prototype needs
to reference an exisiting pointer as shown below.
int *Tmp = NULL
void test(int*& ptr = Tmp);
|
|
| Back to top |
|
 |
Sharad Kala Guest
|
Posted: Fri Apr 29, 2005 12:12 pm Post subject: Re: Template function |
|
|
"John Bufton" <j.bufton (AT) ntlworld (DOT) com> wrote in message
| Quote: | Gurikar wrote:
Can i do this:
void test(int*& ptr = NULL)
{
ptr=0;
}
No.
Prototype definition can have default parameter value, but not the
function itself. Hence the following is OK.
// Function prototype definition with default parameter value
void test(int*& ptr = NULL);
// Function implementation
void test(int *&ptr)
{
ptr = NULL;
}
|
Incorrect. The problem with test function is that you can't bind a non-const
reference to an r-value.
Sharad
|
|
| Back to top |
|
 |
John Bufton Guest
|
Posted: Fri Apr 29, 2005 12:16 pm Post subject: Re: Template function |
|
|
Sharad Kala wrote:
| Quote: | "John Bufton" <j.bufton (AT) ntlworld (DOT) com> wrote in message
Gurikar wrote:
Can i do this:
void test(int*& ptr = NULL)
{
ptr=0;
}
No.
Prototype definition can have default parameter value, but not the
function itself. Hence the following is OK.
// Function prototype definition with default parameter value
void test(int*& ptr = NULL);
// Function implementation
void test(int *&ptr)
{
ptr = NULL;
}
Incorrect. The problem with test function is that you can't bind a non-const
reference to an r-value.
Sharad
|
I couldn't agree more! As already stated, sheer carelessness on my part.
Well spotted anyway. Personally I'm not keen on default parameter values
for reference parameters.
John Bufton
|
|
| Back to top |
|
 |
Gurikar Guest
|
Posted: Fri Apr 29, 2005 12:34 pm Post subject: Re: Template function |
|
|
You are right sharad kala.. Good..
|
|
| Back to top |
|
 |
Axter Guest
|
Posted: Fri Apr 29, 2005 1:53 pm Post subject: Re: Template function |
|
|
Gurikar wrote:
| Quote: | Hello,
Can anyone tell me whats wrong in this?
// template function
template<class T
inline void test(T* ptr)
{
cout<<"Pointer is"<
ptr = NULL;
cout<<"Pointer is"<
}
// Some class
Class A()
{
A();
}
// main function
int main()
{
A* pObj = new A();
test(pObj );
if (pObj == NULL)
{
cout<<"Mem fail"<
}
My question, even after making the pointer pObj = NULL in template
function test, when it comes to main the value of pObj wont be NULL,
Why is this???
Regards
}
|
You're passing the pointer by value, and you need to pass the pointer
by reference or by pointer (pointer to a pointer).
Example code:
// template function
template
inline void test(T*& ptr)
{
cout<<"Pointer is"<
ptr = NULL;
cout<<"Pointer is"<
}
// Some class
class A
{
public:
A(){}
};
int main(int argc, char* argv[])
{
A* pObj = new A;
test(pObj );
if (pObj == NULL)
{
cout<<"Mem fail"<
}
|
|
| Back to top |
|
 |
whisper Guest
|
Posted: Sun May 01, 2005 2:03 pm Post subject: Re: Template function |
|
|
in c++, the arguments of function call are always by value.
eg. void func(void* ptr);
we should know that ptr is by-value, we can feel the changing of the
value of ptr(that is *ptr) out of the scope of func, however, ptr
itself cannot be modified by func as it is by-value.
so ptr is by-value, *ptr is by-ref
if we wanna change ptr, we should change the declare of func to
void func(void** ptr);
|
|
| Back to top |
|
 |
|