常数并发症 [英] Constness Complications
问题描述
这里有两个函数,一个const版本。和非常量版本:
char * GetFirstT(char * p)
{
for(; * p; + + p)
{
如果(* p ==''t'')返回p;
}
返回0;
}
const char * GetFirstT(const char * p)
{< (b; * p; ++ p)
{
if(* p ==''t'')返回p; < br $>
}
返回0;
}
签名main()
{
char the_alphabet [] =" abcdefghijklmnopqrstuvwxyz" ;;
char * p_t = GetFirstT(the_alphabet);
}
看看我如何定义两个函数,有什么方便吗?
我是否必须执行以下操作:
const char * GetFirstT(const char * p)
{
for(; * p; ++ p)
{
如果(* p ==''t'')返回p;
}
返回0;
}
char * GetFirstT(c har * p)
{
返回const_cast< char *>(GetFirstT(p));
}
-JKop
JKop< NU ** @ NULL.NULL>在新闻中写道:i9 ****************** @ news.indigo.ie:
这里是两个函数,const版本,和非常量版本:
char * GetFirstT(char * p)
{
for(; * p; ++ p)
{
如果(* p ==''t'')返回p;
}
返回0;
}
const char * GetFirstT(const char * p)
{
for(; * p; ++ p)
{
如果(* p ==''t'')返回p ;
返回0;
}
签名main()
{char the_alphabet [] =" abcdefghijklmnopqrstuvwxyz" ;;
char * p_t = GetFirstT(the_alphabet);
}
看看我如何定义两个函数,有什么方便吗?
不,你不需要定义它们。见下文。
#include< iostream>
char const * get_first_of(char const * s,char c)
{
for(; * s&& * s!= c; ++ s);
返回s;
}
int main()
{
char s [] =" abcdefghijklmnopqrstuvwxyz" ;;
const char cs [] =" abcdefghijklmnopqrstuvwxyz" ;;
const char * p1 = get_first_of(s,''c'');
const char * p2 = get_first_of(cs,'t'');
std :: cout<< p1<< ''\ n''
<< p2<< ''\ n'';
返回0;
}
当函数指定其参数时作为const,它显然意味着它不会修改它。 Const-specifier可以隐含地挂钩。
干杯。
-
:: bartekd / o2 pl >
::出于混乱而混乱 - 出于混乱而混乱和恐惧
:: - 然后是午餐。
JKop写道:
看看我如何定义两个函数,有什么方便吗?
我是否需要做以下内容:
const char * GetFirstT(const char * p)
{
for(; * p; ++ p)
{
if (* p ==''t'')返回p;
}
返回0;
}
char * GetFirstT(char * p )
{
返回const_cast< char *>(GetFirstT(p));
}
为什么你不用模板吗?
模板< typename CHARTYPE>
CHARTYPE * GetFirstT(CHARTYPE * p)
{
....
}
-
问候,
托比亚斯
JKop写道:这里有两个函数,一个const版本,和非常量版本:
char * GetFirstT(char * p)
{
for(; * p; ++ p)
{
如果(* p ==''t'')返回p;
}
返回0;
}
const char * GetFirstT(const char * p)
{
for(; * p; ++ p)
{
如果(* p ==''t'')返回p ;
返回0;
}
签名main()
{char the_alphabet [] =" abcdefghijklmnopqrstuvwxyz" ;;
char * p_t = GetFirstT(the_alphabet);
}
看看我如何定义两个函数,有什么方便吗?<我是否必须做以下事情:
const char * GetFirstT(const char * p)
{
for(; * p; ++ p)
{
如果(* p ==''t'')返回p;
}
返回0;
}
> char * GetFirstT(char * p)
{
返回const_cast< char *>(GetFirstT(p));
}
-JKop
地狱o,
如何使用模板?
模板< class charT>
charT * GetFirstT(charT * p)
{
for(; * P; ++ p)
{
如果(* p ==''t'')返回p;
}
返回0;
}
签名main()
{
char the_alphabet [] =" abcdefghijklmnopqrstuvwxyz";
char * p_t = GetFirstT< char>(the_alphabet);
}
这样你甚至可以为wchar和const wchar创建GetFirstT()。即使
更好会强制使用character_traits,但这对我来说太过分了。
也许别人会知道...
Ales
Here''s two functions, a "const version" and a "non-const version":
char* GetFirstT(char* p)
{
for ( ; *p; ++p)
{
if ( *p == ''t'' ) return p;
}
return 0;
}
const char* GetFirstT(const char* p)
{
for ( ; *p; ++p)
{
if ( *p == ''t'' ) return p;
}
return 0;
}
signed main()
{
char the_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
char* p_t = GetFirstT(the_alphabet);
}
See how I have to define two functions, is there any handy around that?
Would I have to do the following:
const char* GetFirstT(const char* p)
{
for ( ; *p; ++p)
{
if ( *p == ''t'' ) return p;
}
return 0;
}
char* GetFirstT(char* p)
{
return const_cast<char*>( GetFirstT(p) );
}
-JKop
JKop <NU**@NULL.NULL> wrote in news:i9******************@news.indigo.ie:
Here''s two functions, a "const version" and a "non-const version":
char* GetFirstT(char* p)
{
for ( ; *p; ++p)
{
if ( *p == ''t'' ) return p;
}
return 0;
}
const char* GetFirstT(const char* p)
{
for ( ; *p; ++p)
{
if ( *p == ''t'' ) return p;
}
return 0;
}
signed main()
{
char the_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
char* p_t = GetFirstT(the_alphabet);
}
See how I have to define two functions, is there any handy around that?
No, you dont need to define both of them. See below.
#include <iostream>
char const* get_first_of(char const* s, char c)
{
for (; *s && *s != c; ++s);
return s;
}
int main()
{
char s[] = "abcdefghijklmnopqrstuvwxyz";
const char cs[] = "abcdefghijklmnopqrstuvwxyz";
const char* p1 = get_first_of(s, ''c'');
const char* p2 = get_first_of(cs, ''t'');
std::cout << p1 << ''\n''
<< p2 << ''\n'';
return 0;
}
When a function specifies its argument as const, it clearly means that it
won''t modify that. Const-specifier can be implicitly hooked on.
Cheers.
--
:: bartekd / o2 pl
:: "out of confusion comes chaos -- out of chaos comes confusion and fear
:: -- then comes lunch."
JKop wrote:
See how I have to define two functions, is there any handy around that?
Would I have to do the following:
const char* GetFirstT(const char* p)
{
for ( ; *p; ++p)
{
if ( *p == ''t'' ) return p;
}
return 0;
}
char* GetFirstT(char* p)
{
return const_cast<char*>( GetFirstT(p) );
}
Why don''t you use templates?
template<typename CHARTYPE>
CHARTYPE* GetFirstT(CHARTYPE* p)
{
....
}
--
Regards,
Tobias
JKop wrote:Here''s two functions, a "const version" and a "non-const version":
char* GetFirstT(char* p)
{
for ( ; *p; ++p)
{
if ( *p == ''t'' ) return p;
}
return 0;
}
const char* GetFirstT(const char* p)
{
for ( ; *p; ++p)
{
if ( *p == ''t'' ) return p;
}
return 0;
}
signed main()
{
char the_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
char* p_t = GetFirstT(the_alphabet);
}
See how I have to define two functions, is there any handy around that?
Would I have to do the following:
const char* GetFirstT(const char* p)
{
for ( ; *p; ++p)
{
if ( *p == ''t'' ) return p;
}
return 0;
}
char* GetFirstT(char* p)
{
return const_cast<char*>( GetFirstT(p) );
}
-JKop
Hello,
what about using template?
template <class charT>
charT* GetFirstT(charT* p)
{
for ( ; *p; ++p)
{
if ( *p == ''t'' ) return p;
}
return 0;
}
signed main()
{
char the_alphabet[] = "abcdefghijklmnopqrstuvwxyz";
char* p_t = GetFirstT<char>(the_alphabet);
}
This way you can create GetFirstT() even for wchar and const wchar. Even
better would force use of character_traits, but this is too much for me.
Maybe someone else will know...
Ales
这篇关于常数并发症的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!