将int * *传递给const作为函数 [英] passing int * * to function as const

查看:81
本文介绍了将int * *传递给const作为函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这个问题有意义;如果没有,请纠正我的想法

:)


如果我想在运行时创建一维数组,我会写


int * myArray = new int [size];


并使用原型将该数组传递给函数


void foo1(int *);





void foo2(const int *);


取决于我是否希望该函数能够修改数组中的

数据。

这两个似乎都可以正常工作。


现在我想对int * *做同样的事情。我创建了''table''作为


int * * myTable = new int * [rows];

for(int i = 0; i< ;行; ++ i)

myTable [i] = new int [cols];


并将其发送给带原型的函数


void bar1(int * *);





void bar2(const int * *);


一切都很好,除了最后一行:编译器抱怨

无法从int * *转换为const int * *;转换失去限定符

或类似的东西。


如何以这种方式将此int * *''table''发送到函数

函数无法修改数据 - 也就是说,将它作为const发送,就像我可以用

一个int *''数组''? br />

使用std :: vector而不是回答我的问题,所以请不要
建议它。


-

John Goulden
jg ****** @ okcu。 edu


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

Hopefully this question will make sense; if not, please correct my thinking
:)

If I wish to create a one-dimensional array at run time, I write

int * myArray = new int [size];

and pass that array to functions using prototypes

void foo1 ( int * );

or as

void foo2 (const int *);

depending on whether or not I wish the function to be able to modify the
data in the array.
Both of these seem to work just fine.

Now I wish to do the same with int * *. I create the ''table'' as

int * * myTable = new int * [rows];
for(int i = 0; i < rows; ++i)
myTable[i] = new int [cols];

and send it to functions with prototypes

void bar1 ( int * *);

or

void bar2 ( const int * *);

Everything works great except that last line: the compilers complain about
"cannot convert from int * * to const int * *; conversion loses qualifiers"
or something to that effect.

How can I send this int * * ''table'' to a function in such a way that the
function can''t modify the data - that is, send it as const, as I can do with
an int * ''array'' ?

Using std::vector instead doesn''t answer my question, so please don''t
suggest it.

--
John Goulden
jg******@okcu.edu


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

John D. Goulden写道:
John D. Goulden wrote:
希望这个问题有意义;如果没有,请纠正我的想法
:)

如果我想在运行时创建一维数组,我写一下

int * myArray = new int [size];

并使用原型将该数组传递给函数

void foo1(int *);

或作为

void foo2(const int *);

取决于我是否希望该函数能够修改数组中的
数据。
两者都是这些似乎工作得很好。

现在我希望对int * *做同样的事情。我将''table''创建为

int * * myTable = new int * [rows];
for(int i = 0; i< rows; ++ i)
myTable [i] = new int [cols];

并将其发送到带原型的函数

void bar1(int * *);
<无效bar2(const int * *);

一切都很好,除了最后一行:编译器抱怨
无法转换自int * * to const int * *;转换失去限定符
或其他类似的东西。

如何将这个int * *''table''发送到一个函数中,以便
函数可以不修改数据 - 也就是说,将它作为const发送,就像我用
一个int *''数组''那样?


请参阅:

http://www.parashift.com/c++-faq-lit...html#faq-18.15


(你*先*先阅读常见问题解答,不是吗?)

使用std :: vector而不是回答我的问题,所以请不要<建议吧。
Hopefully this question will make sense; if not, please correct my thinking
:)

If I wish to create a one-dimensional array at run time, I write

int * myArray = new int [size];

and pass that array to functions using prototypes

void foo1 ( int * );

or as

void foo2 (const int *);

depending on whether or not I wish the function to be able to modify the
data in the array.
Both of these seem to work just fine.

Now I wish to do the same with int * *. I create the ''table'' as

int * * myTable = new int * [rows];
for(int i = 0; i < rows; ++i)
myTable[i] = new int [cols];

and send it to functions with prototypes

void bar1 ( int * *);

or

void bar2 ( const int * *);

Everything works great except that last line: the compilers complain about
"cannot convert from int * * to const int * *; conversion loses qualifiers"
or something to that effect.

How can I send this int * * ''table'' to a function in such a way that the
function can''t modify the data - that is, send it as const, as I can do with
an int * ''array'' ?
Please see:

http://www.parashift.com/c++-faq-lit...html#faq-18.15

(you *did* read the FAQ first, didn''t you?)

Using std::vector instead doesn''t answer my question, so please don''t
suggest it.




HTH,

--ag


-

Artie Gold - 德克萨斯州奥斯汀

哦,对于常规旧垃圾邮件的美好时光。



HTH,
--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.




" John D. Goulden" < JG *********** @ goulden.org>在消息中写道

news:bn ********* @ enews2.newsguy.com ...

"John D. Goulden" <jg***********@goulden.org> wrote in message
news:bn*********@enews2.newsguy.com...
现在我想做与int * *相同。我创建''table''作为
int * * myTable = new int * [rows];
for(int i = 0; i< rows; ++ i)
myTable [i] = new int [cols];
并将其发送给带有原型的函数
void bar1(int * *);

void bar2(const int * *);
一切都很好,除了最后一行:编译器抱怨
无法从int * *转换为const int * *;转换损失
限定符或者那种效果。
如何将这个int * *''table''发送到函数中,使得
函数无法修改数据 - 也就是说,将它作为const发送,因为我可以使用int *''数组'做
Now I wish to do the same with int * *. I create the ''table'' as int * * myTable = new int * [rows];
for(int i = 0; i < rows; ++i)
myTable[i] = new int [cols]; and send it to functions with prototypes void bar1 ( int * *); or void bar2 ( const int * *); Everything works great except that last line: the compilers complain about
"cannot convert from int * * to const int * *; conversion loses qualifiers" or something to that effect. How can I send this int * * ''table'' to a function in such a way that the
function can''t modify the data - that is, send it as const, as I can do with an int * ''array'' ?




将bar2定义为


void bar2(const int * const *);


一般来说,没有从T **到const T **的转换,因为要做

否则会打开一个类型系统中的洞。我相信其他人会更加详细地解释,我今天感觉很懒,所以我会留给他们。

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



Define bar2 as

void bar2 (const int *const *);

In general, there is no conversion from T** to const T**, because to do
otherwise would open a hole in the type system. I am sure that others will
explain in more detail, and I feel lazy today, so I''ll leave it to them.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


John D. Goulden写道:
John D. Goulden wrote:
希望这个问题有意义;如果没有,请更正我的想法
:)

如果我想在运行时创建一维数组,我写的是

int * myArray = new int [size];

并使用原型将该数组传递给函数

void foo1(int *);

或as

void foo2(const int *);

取决于我是否希望该函数能够修改数组中的
数据。这两个似乎都运行得很好。

现在我希望对int * *做同样的事情。我将''table''创建为

int * * myTable = new int * [rows];
for(int i = 0; i< rows; ++ i)
myTable [i] = new int [cols];

并将其发送到带原型的函数

void bar1(int * *);
<无效bar2(const int * *);

一切都很好,除了最后一行:编译器抱怨
无法转换自int * * to const int * *;转换失败
限定符或类似的东西。

如何将这个int * *''table''发送到一个函数,使得
函数无法修改数据 - 是的,发送它作为const,因为我可以做一个int *''数组''

使用std :: vector而不是回答我的问题,所以请不要't
建议。
Hopefully this question will make sense; if not, please correct my
thinking
:)

If I wish to create a one-dimensional array at run time, I write

int * myArray = new int [size];

and pass that array to functions using prototypes

void foo1 ( int * );

or as

void foo2 (const int *);

depending on whether or not I wish the function to be able to modify the
data in the array.
Both of these seem to work just fine.

Now I wish to do the same with int * *. I create the ''table'' as

int * * myTable = new int * [rows];
for(int i = 0; i < rows; ++i)
myTable[i] = new int [cols];

and send it to functions with prototypes

void bar1 ( int * *);

or

void bar2 ( const int * *);

Everything works great except that last line: the compilers complain about
"cannot convert from int * * to const int * *; conversion loses
qualifiers" or something to that effect.

How can I send this int * * ''table'' to a function in such a way that the
function can''t modify the data - that is, send it as const, as I can do
with an int * ''array'' ?

Using std::vector instead doesn''t answer my question, so please don''t
suggest it.




只需使用

const int * const *

to使中间指针也指向const。


alex。


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



just use
const int * const *
to make the intermediate pointer point to const, too.

alex.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于将int * *传递给const作为函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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