引用数组,不引用引用数组! [英] Reference to array, NOT array of references!
问题描述
模板< class T>
inline T& NthArrayMember(T&(array []),size_t i)
{
返回数组[i-1];
}
模板< class T>
inline const T& NthArrayMember(const T&(array []),size_t i)
{
返回数组[i-1];
}
G ++:作为参考数组的'array''的声明
我尝试过五种不同的括号组合,但我不能
得到一个数组的引用!
这样就可以这样使用:
int main()
{
int blah [50];
NthArrayMember(blah,45)= 56; // 45th member = 56
extern void SomeFunc(int);
SomeFunc(NthArrayMember(blah,34)); //通过第34个成员
}
-JKop
template<class T>
inline T& NthArrayMember( T & (array[]),size_t i)
{
return array[i - 1];
}
template<class T>
inline const T& NthArrayMember( const T & (array[]),size_t i)
{
return array[i - 1];
}
G++: declaration of `array'' as array of references
I''ve tried about five different parenthesis combinations, but I just can''t
get a reference to an array!
It''s to be used as so:
int main()
{
int blah[50];
NthArrayMember(blah,45) = 56; //45th member = 56
extern void SomeFunc(int);
SomeFunc( NthArrayMember(blah,34) ); //Passes 34th member
}
-JKop
推荐答案
当然,我觉得这个有用,你觉得以下
会更有效率,因为临时是不必要的:
模板< class T>
inline T& NthArrayMember(T&(array []),size_t i)
{
返回数组[i - = 1]; //而不仅仅是 -
}
Granted I get this to work, do you think the following
would be more efficent in that a temporary is unnecessary:
template<class T>
inline T& NthArrayMember( T & (array[]),size_t i)
{
return array[i -= 1]; //As opposed to just -
}
" JKop" < NU ** @ NULL.NULL>在消息中写道
news:_E ***************** @ news.indigo.ie ...
"JKop" <NU**@NULL.NULL> wrote in message
news:_E*****************@news.indigo.ie...
template< T级>
内联T& NthArrayMember(T&(array []),size_t i)
{
返回数组[i - 1];
}
模板< class T> <内联const T& NthArrayMember(const T&(array []),size_t i)
{
返回数组[i - 1];
}
G ++:声明` array''作为引用数组
我尝试过五种不同的括号组合,但我不能获得对数组的引用!
int main()
{
int blah [50];
NthArrayMember(blah ,45)= 56; // 45th member = 56
extern void SomeFunc(int);
SomeFunc(NthArrayMember(blah,34)); //通过第34个成员
}
template<class T>
inline T& NthArrayMember( T & (array[]),size_t i)
{
return array[i - 1];
}
template<class T>
inline const T& NthArrayMember( const T & (array[]),size_t i)
{
return array[i - 1];
}
G++: declaration of `array'' as array of references
I''ve tried about five different parenthesis combinations, but I just can''t
get a reference to an array!
It''s to be used as so:
int main()
{
int blah[50];
NthArrayMember(blah,45) = 56; //45th member = 56
extern void SomeFunc(int);
SomeFunc( NthArrayMember(blah,34) ); //Passes 34th member
}
鉴于数组首先不是一个类型,我不知道如何你
可以创建它的引用。你甚至不能创建一个指向数组的指针
(只是指向它的第一个元素的指针)。并且,如果打算将其用作
描述,为什么不呢:
blah [45] = 56;
SomeFunc(等等[34]);
?
-Howard
Given that an array is not a type in the first place, I don''t see how you
can create a reference to it. You can''t even create a pointer to an array
(just a pointer to its first element). And, if the intent is to use it as
described, why not just:
blah[45] = 56;
SomeFunc(blah[34]);
?
-Howard
霍华德发布:
Howard posted:
为什么不呢:
blah [45] = 56;
SomeFunc(等等[34]);
?
why not just:
blah[45] = 56;
SomeFunc(blah[34]);
?
设置第46个成员
通过第35个成员。
显然我可以写:
SomeFunc(等等[33]);
但是我正在编写一个相当长的功能和计算机,我认为
不同:我将第一个成员与1相关联,而计算机
将它与我肯定会在某个地方滑倒!
无论如何,这里是最新版本:
模板< class T,class R> ;
内联T& NthArrayMember(T(& array)[],R i)
{
返回数组[ - i];
}
模板< class T,class R>
inline const T& NthArrayMember(const T(& array)[],R i)
{
返回数组[ - i];
}
g ++ tester.cpp -ansi -pedantic -Wall -o t.exe
tester.cpp:83:参数`array''包含对数组的引用未知
绑定`
T []''
tester.cpp:89:参数`array''包含对未知数组的引用< br $> b $ b绑定`
const T []''
tester.cpp:在函数`int main()''中:
tester.cpp:99:`GetNthArrayMember''未声明(首次使用此功能)
tester.cpp:99 :(每个未声明的标识符仅报告一次
函数出现在。)
有什么想法吗?
-JKop
The sets the 46th member
That passes the 35th member.
Obviously I could write:
SomeFunc(blah[33]);
But I''m writing a pretty lengthly function and the computer and I think
differently: I associate the first member with 1, while the computer
associates it with 0. I''m bound to slip-up somewhere!
Anyway, here''s the latest version:
template<class T, class R>
inline T& NthArrayMember( T (&array)[],R i)
{
return array[--i];
}
template<class T, class R>
inline const T& NthArrayMember( const T (& array)[],R i)
{
return array[--i];
}
g++ tester.cpp -ansi -pedantic -Wall -o t.exe
tester.cpp:83: parameter `array'' includes reference to array of unknown
bound `
T[]''
tester.cpp:89: parameter `array'' includes reference to array of unknown
bound `
const T[]''
tester.cpp: In function `int main()'':
tester.cpp:99: `GetNthArrayMember'' undeclared (first use this function)
tester.cpp:99: (Each undeclared identifier is reported only once for each
function it appears in.)
Any ideas?
-JKop
这篇关于引用数组,不引用引用数组!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!