模板类问题(如何处理char *) [英] Templated Classes question (what to do with char*)

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

问题描述

大家好(第一次来电,长时间听众),

我偶然发现了一个我还没弄清楚的问题,

虽然我肯定当我弄明白时,我会踢自己。这是:


我需要创建一个接受任何类型的模板化类。很简单

我就像这样:


模板< typename类型>

class Foo

{

类型数据;

Foo(类型d){data = d);

~Foo(void){}

void SetData(类型d){data = d; }

}


但是如果类型是char *怎么办?有没有一种方法可以告诉它和

因此strcpy进入数据而不是直接复制指针所以

当输入的字符串输入SetData失去范围时我可以保留

呢?希望这是有道理的。谢谢!

解决方案

Ro ********** @ gmail.com 写道:


大家好(第一次来电,长时间听众),


我偶然发现了一个我还没弄清楚的问题,

虽然我确定当我弄明白时我会踢自己。这是:


我需要创建一个接受任何类型的模板化类。很简单

我就像这样:


模板< typename类型>

class Foo

{

类型数据;

Foo(类型d){data = d);



我更喜欢初始化:


Foo(类型d)

:数据(d )

{}


~Foo(void){}

void SetData(Type d){data = d; }



Setter和Getter函数通常都是气味。


}


缺少" ;;"


但是如果类型是char *怎么办?有没有一种方法可以告诉它和

因此strcpy进入数据而不是直接复制指针所以

当输入的字符串输入SetData失去范围时我可以保留

呢?希望这是有道理的。



当然可以:你可以使用部分专业化:


模板<>

class Foo< char * {


char * data;


Foo(char * str):

:data(new char [strlen(str)+ 1])

// FIXME:可能一个人关闭

{

strcpy(我忘记了参数顺序)

// FIXME:我不知道strcpy是否负责终止0.

}


.. 。


};


或类似的东西。


但请注意,


a)你需要为签名的char和unsigned char执行此操作。

b)默认实现很可能对任何

指针类型。

您可能想查看一下enable_if和disable_if从boost到

看看是否可以防止Foo< T *为实例化任何T但是

char / signed char / unsigned char。


Best


Kai-Uwe Bux


2月3日晚上8:54,Kai-Uwe Bux< jkherci ... @ gmx.netwrote :


Robert.Ho ... @ gmail.com写道:


全部(第一次)来电,长时间听众),


我偶然发现了一个我还没弄清楚的问题,

虽然我确定当我弄明白时我会踢自己。这是:


我需要创建一个接受任何类型的模板化类。简单

足够我,但是这样:


模板< typename类型>

class Foo

{

类型数据;

Foo(类型d){data = d);



我更喜欢初始化:


Foo(类型d)

:数据(d )

{}


~Foo(void){}

void SetData(Type d){data = d; }



Setter和Getter函数通常都是气味。


}


缺失""


但是如果类型是char *怎么办?有没有一种方法可以告诉它和

因此strcpy进入数据而不是直接复制指针所以

当输入的字符串输入SetData失去范围时我可以保留

呢?希望这是有道理的。



当然可以:你可以使用部分专业化:


模板<>

class Foo< char * {


char * data;


Foo(char * str):

:data(new char [strlen(str)+ 1])

// FIXME:可能一个人关闭

{

strcpy(我忘记了参数顺序)

// FIXME:我不知道strcpy是否负责终止0.

}


.. 。


};


或类似的东西。


但请注意,


a)你需要为签名的char和unsigned char执行此操作。

b)默认实现很可能对任何

指针类型。


你可能想看一下enable_if和disable_if从boost到

看你是否可以阻止Foo< T *将被实例化为任何T但是

char / signed char / unsigned char。


Best


Kai-Uwe Bux



我不明白,做部分我随时可以专业化

一个类Foo< char *它将使用该专业化和任何其他时间

它将使用Foo< Type>。你是这个意思吗?所以基本上硬代码

指针类型的模板函数?


在文章< 11 ***** *****************@q2g2000cwa.googlegroups。 com>,
Ro**********@gmail.com 说...


大家好(第一次来电,长时间听众),


我偶然发现我还没弄明白的问题,

虽然我确定当我弄明白时我会踢自己。这是:


我需要创建一个接受任何类型的模板化类。很简单

我就像这样:


模板< typename类型>

class Foo

{

类型数据;

Foo(类型d){data = d);

~Foo(void){}

void SetData(类型d){data = d; }

}


但是如果类型是char *怎么办?有没有一种方法可以告诉它和

因此strcpy进入数据而不是直接复制指针所以

当输入的字符串输入SetData失去范围时我可以保留

呢?希望这是有道理的。谢谢!



当然 - 您可以明确地将您的模板专门化为类型char *,

并使显式专业化做正确的事情。您将使用上面的代码开始

(在修复错别字之后,例如在ctor中),然后

添加明确的专业化:


模板<>

类Foo< char * {

// ...

};


使这个代码异常安全将是非常重要的。除非

你对异常安全很有经验,否则我建议你在实现这段代码之前阅读_Exceptional C ++ _的8-13项目。


-

后来,

杰瑞。


宇宙是虚构的自己的想象力。


Hi All (first time caller, long time listener),
I''ve stumbled across a problem that I have yet to figure out,
although Im sure I''ll kick myself when I figure it out. Here it is:

I need to create a templated class that accepts any type. Easy
enough I though, like this:

template<typename Type>
class Foo
{
Type data;
Foo(Type d){ data = d);
~Foo(void){}
void SetData(Type d){ data = d; }
}

But what if the type is char*? Is there a way I can tell it is and
therefore strcpy into data instead of straight copy of the pointer so
that when the inputted string into SetData loses scope I can retain
it? Hopefully that makes sense. Thanks!

解决方案

Ro**********@gmail.com wrote:

Hi All (first time caller, long time listener),
I''ve stumbled across a problem that I have yet to figure out,
although Im sure I''ll kick myself when I figure it out. Here it is:

I need to create a templated class that accepts any type. Easy
enough I though, like this:

template<typename Type>
class Foo
{
Type data;
Foo(Type d){ data = d);

I would prefer initialization:

Foo ( Type d )
: data ( d )
{}

~Foo(void){}
void SetData(Type d){ data = d; }

Setter and Getter functions are usually a smell.

}

missing ";"

But what if the type is char*? Is there a way I can tell it is and
therefore strcpy into data instead of straight copy of the pointer so
that when the inputted string into SetData loses scope I can retain
it? Hopefully that makes sense.

Sure does: You can use a partial specialization:

template <>
class Foo< char* {

char* data;

Foo ( char* str ) :
: data ( new char [ strlen( str ) + 1 ] )
//FIXME: maybe off by one
{
strcpy( I forgot the argument order )
// FIXME: I don''t know whether strcpy takes care of terminating 0.
}

...

};

or something like that.

Note however, that

a) you would need to do this for signed char and unsigned char, too.
b) The default implementation is very likely to do the wrong thing for any
pointer type.
You might want to have a look into enable_if and disable_if from boost to
see whether you can prevent Foo<T*to be instantiated for any T but
char/signed char/unsigned char.

Best

Kai-Uwe Bux


On Feb 3, 8:54 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:

Robert.Ho...@gmail.com wrote:

Hi All (first time caller, long time listener),

I''ve stumbled across a problem that I have yet to figure out,
although Im sure I''ll kick myself when I figure it out. Here it is:

I need to create a templated class that accepts any type. Easy
enough I though, like this:

template<typename Type>
class Foo
{
Type data;
Foo(Type d){ data = d);


I would prefer initialization:

Foo ( Type d )
: data ( d )
{}

~Foo(void){}
void SetData(Type d){ data = d; }


Setter and Getter functions are usually a smell.

}


missing ";"

But what if the type is char*? Is there a way I can tell it is and
therefore strcpy into data instead of straight copy of the pointer so
that when the inputted string into SetData loses scope I can retain
it? Hopefully that makes sense.


Sure does: You can use a partial specialization:

template <>
class Foo< char* {

char* data;

Foo ( char* str ) :
: data ( new char [ strlen( str ) + 1 ] )
//FIXME: maybe off by one
{
strcpy( I forgot the argument order )
// FIXME: I don''t know whether strcpy takes care of terminating 0.
}

...

};

or something like that.

Note however, that

a) you would need to do this for signed char and unsigned char, too.
b) The default implementation is very likely to do the wrong thing for any
pointer type.

You might want to have a look into enable_if and disable_if from boost to
see whether you can prevent Foo<T*to be instantiated for any T but
char/signed char/unsigned char.

Best

Kai-Uwe Bux

I dont understand, by doing the partial specialization anytime I made
a class Foo<char*it would use that specialization and any other time
it would use Foo<Type>. Is that what you mean? So basically hard code
the template function for types that are pointers?


In article <11**********************@q2g2000cwa.googlegroups. com>,
Ro**********@gmail.com says...

Hi All (first time caller, long time listener),
I''ve stumbled across a problem that I have yet to figure out,
although Im sure I''ll kick myself when I figure it out. Here it is:

I need to create a templated class that accepts any type. Easy
enough I though, like this:

template<typename Type>
class Foo
{
Type data;
Foo(Type d){ data = d);
~Foo(void){}
void SetData(Type d){ data = d; }
}

But what if the type is char*? Is there a way I can tell it is and
therefore strcpy into data instead of straight copy of the pointer so
that when the inputted string into SetData loses scope I can retain
it? Hopefully that makes sense. Thanks!

Sure -- you can explicitly specialize your template over type char *,
and have the explicit specialization do the right thing. You''d start
with the code above (after fixing typos, such as in the ctor), and then
add an explicit specialization:

template<>
class Foo<char *{
// ...
};

Making this code exception safe WILL be decidedly non-trivial. Unless
you''re quite experienced with exception safety, I''d recommend reading
items 8-13 of _Exceptional C++_ before you implement this code.

--
Later,
Jerry.

The universe is a figment of its own imagination.


这篇关于模板类问题(如何处理char *)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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