模板功能 [英] Template function

查看:62
本文介绍了模板功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

有人能告诉我这里有什么问题吗?


//模板功能

模板< class T> ;

inline void test(T * ptr)

{

cout<<"" Pointer is"<< ptr<< ; endl;

ptr = NULL;

cout<<"指针是"<<< ptr<< endl;

}


//一些课程

A班()

{

A();

}


//主要功能

int main()

{

A * pObj = new A();


test(pObj);


if(pObj == NULL)

{

cout<<"" Mem fail"<< endl;

}


我的问题,即使在模板中使用指针pObj = NULL

函数测试之后,当涉及到main时,pObj的值也不会为NULL,

为什么这样????


问候

}

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
}

推荐答案

" Gurikar" < MS ******* @ gmail.com>写道:
"Gurikar" <ms*******@gmail.com> writes:
你好,
谁能告诉我这里有什么问题?
...
我的问题,即使在指针pObj =模板中的NULL
函数测试,当说到main时pObj的值不会为NULL,
为什么这个?
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现在忘了模板,看看类似


#include< iostream>

void test(int * ptr)

{

ptr = 0;

}


int main(){

int * pObj = new int();

test(pObj);

if(pObj == 0)

std :: cout<<" ; Mem失败"<<< std :: endl;

}



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


Gurikar写道:
Gurikar wrote:
你好,
任何人都可以告诉我这里有什么问题吗?

//模板功能
模板< class T>
内联空洞测试(T * ptr)
{
cout<<"" Pointer is"<<< ptr<< endl;
ptr = NULL ;
cout<<"指针是"<< ptr<< endl;
}
//某些类
A类()
{
A();
}
//主要功能
int main()
{
A * pObj =新的A();

测试(pObj);

if(pObj == NULL)
{
cout<<<" Mem fail" ;<< endl;
}
我的问题,即使在模板
函数测试中指针pObj = NULL之后,当涉及到pObj的主要值时也不会是NULL,
为什么这个?

问候
}
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
}




指针按值传递。因此,调用

函数中的指针值保持不变。如果你想要调用函数的

指针,请尝试通过引用传递。


John Bufton



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

为什么这个??????

why is this??????


这篇关于模板功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆