Template function

G

Gurikar

Hello,
Can anyone tell me whats wrong in this?

// template function
template<class T>
inline void test(T* ptr)
{
cout<<"Pointer is"<<ptr<<endl;
ptr = NULL;
cout<<"Pointer is"<<ptr<<endl;
}

// Some class
Class A()
{
A();
}

// main function
int main()
{
A* pObj = new A();

test(pObj );

if (pObj == NULL)
{
cout<<"Mem fail"<<endl;
}

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
}
 
T

Tim Love

Gurikar said:
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"<<std::endl;
}
 
J

John Bufton

Gurikar said:
Hello,
Can anyone tell me whats wrong in this?

// template function
template<class T>
inline void test(T* ptr)
{
cout<<"Pointer is"<<ptr<<endl;
ptr = NULL;
cout<<"Pointer is"<<ptr<<endl;
}

// Some class
Class A()
{
A();
}

// main function
int main()
{
A* pObj = new A();

test(pObj );

if (pObj == NULL)
{
cout<<"Mem fail"<<endl;
}

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
 
S

Sharad Kala

Gurikar said:
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.
 
J

John Bufton

Gurikar said:
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
 
S

Sharad Kala

Gurikar said:
why is this??????

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
 
J

John Bufton

John said:
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);
 
S

Sharad Kala

John Bufton said:
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
 
J

John Bufton

Sharad said:
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
 
A

Axter

Gurikar said:
Hello,
Can anyone tell me whats wrong in this?

// template function
template<class T>
inline void test(T* ptr)
{
cout<<"Pointer is"<<ptr<<endl;
ptr = NULL;
cout<<"Pointer is"<<ptr<<endl;
}

// Some class
Class A()
{
A();
}

// main function
int main()
{
A* pObj = new A();

test(pObj );

if (pObj == NULL)
{
cout<<"Mem fail"<<endl;
}

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<class T>
inline void test(T*& ptr)
{
cout<<"Pointer is"<<ptr<<endl;
ptr = NULL;
cout<<"Pointer is"<<ptr<<endl;
}
// Some class
class A
{
public:
A(){}
};

int main(int argc, char* argv[])
{
A* pObj = new A;

test(pObj );


if (pObj == NULL)
{
cout<<"Mem fail"<<endl;
}
 
W

whisper

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top