返回指向固定大小数组的数组的指针C ++ [英] Returning a pointer to array of fixed sized arrays C++

查看:118
本文介绍了返回指向固定大小数组的数组的指针C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在任何地方搜索,但是由于这是一个令人困惑的问题,因此无法找到所需的内容.我正在尝试创建函数/方法,但是我不知道如何指定其返回类型,应该是:

I tried searching everywhere, but because it is such a perplexing question, I wasn't able to find what I was looking for. I am trying to create function/method but I don't know how to specify its return type, which should be:

double(*)[3]

我希望能够使用这样的查询:

I want to be able to use a query like this one:

double R[3][3];
query ( &output, R );

但我没有向量std::vector<double> R_vect (9);,而是这样做了:

but instead of R[3][3], I have a vector std::vector<double> R_vect (9); and I do this:

query ( &output, reinterpret_cast<double(*)[3]> (R_vect.data()) );

这是一团糟,所以我想实现一个使其可读的函数,例如:

which is a mess, so I wanted to implement a function to make it readable, say:

ReturnType Cast ( const std::vector<double>& R_vect ) {
  return reinterpret_cast<double(*)[3]> (R_vect.data());
}

但是我不能指定返回类型. 我使用了typedef,它可以正常工作:

but I can't specify the return type. I used typedef, and it works:

typedef double DesiredCast[3];
DesiredCast* Cast ( ... ) { ... }

但是我仍然很好奇没有typedef时该怎么做.

but I am still curious how to do it without typedefs.

推荐答案

您应始终键入此类的复杂返回类型,而不是要求读者解开它们. (或重新设计,以便您没有复杂的类型!)

You should always typedef complicated return types like these, rather than require the reader to untangle them. (or redesign so you don't have complicated types!)

但是您可以按照模式进行操作.要声明这种类型的变量,您可以

But you can just follow the pattern. To declare a variable of this type you would do

double (*var)[3];

要使它起作用,您只需将常用装饰放在名称旁边的常用位置即可,尽管看上去很恐怖.例如使用名为zint自变量:

and to make it a function you just put the usual decoration in the usual place next to the name, despite horrible it seems. e.g. with an int argument named z:

double (*func(int z))[3]
{
    // ...
}

偶然地, cdecl 就会为您完成.

Incidentally, cdecl will do that for you, once you learn its language.

这篇关于返回指向固定大小数组的数组的指针C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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