C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Template function

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Gurikar
Guest





PostPosted: Fri Apr 29, 2005 11:25 am    Post subject: Template function Reply with 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
}

Back to top
Tim Love
Guest





PostPosted: Fri Apr 29, 2005 11:38 am    Post subject: Re: Template function Reply with quote



"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





PostPosted: Fri Apr 29, 2005 11:45 am    Post subject: Re: Template function Reply with quote



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





PostPosted: Fri Apr 29, 2005 11:48 am    Post subject: Re: Template function Reply with quote

why is this??????

Back to top
Gurikar
Guest





PostPosted: Fri Apr 29, 2005 11:51 am    Post subject: Re: Template function Reply with quote

Can i do this:

void test(int*& ptr = NULL)
{
ptr=0;

}

Back to top
Sharad Kala
Guest





PostPosted: Fri Apr 29, 2005 12:01 pm    Post subject: Re: Template function Reply with quote


"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





PostPosted: Fri Apr 29, 2005 12:02 pm    Post subject: Re: Template function Reply with quote

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





PostPosted: Fri Apr 29, 2005 12:02 pm    Post subject: Re: Template function Reply with quote


"Gurikar" <msgurikar (AT) gmail (DOT) com> wrote in message

Quote:
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



Back to top
John Bufton
Guest





PostPosted: Fri Apr 29, 2005 12:10 pm    Post subject: Re: Template function Reply with quote

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





PostPosted: Fri Apr 29, 2005 12:12 pm    Post subject: Re: Template function Reply with quote


"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





PostPosted: Fri Apr 29, 2005 12:16 pm    Post subject: Re: Template function Reply with quote

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





PostPosted: Fri Apr 29, 2005 12:34 pm    Post subject: Re: Template function Reply with quote

You are right sharad kala.. Good..

Back to top
Axter
Guest





PostPosted: Fri Apr 29, 2005 1:53 pm    Post subject: Re: Template function Reply with quote

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





PostPosted: Sun May 01, 2005 2:03 pm    Post subject: Re: Template function Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.