- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的团队(包括我自己)是 C++ 的新手。我们新开发的一部分是一个 C++ 函数,它需要与一个以数组作为输入的 C 函数接口(interface)。类似以下构造的东西是为了实现这一点:
#include "stdio.h"
void the_c_function(double *array, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d: %g\n", i, array[i]);
}
}
void the_cpp_wrapper(double& dref, int len)
{
the_c_function(&dref, len);
}
int main()
{
const int LEN = 4;
double dbl_array[LEN] = { 3,4,5,6 };
the_cpp_wrapper(dbl_array[0], LEN);
return 0;
}
编译后,它按预期工作:它打印数组的内容:
0: 3
1: 4
2: 5
3: 6
但这对我来说几乎不合法,或者充其量应该被劝阻。
这是合法的 C++ 吗,即是否保证指向数组引用的指针指向原始数组?
有什么理由可以这样做而不是直接使用指针而不是使用引用作为中间值吗?
最佳答案
My team (including myself) is new to C++. ...
[...]
... something that should be discouraged.
您现在应该养成使用标准 C++ 库的习惯,在您的情况下最好的选择是 std::vector
:
#include <stdio.h>
#include <stdlib>
#include <vector>
void the_c_function(const double *array, size_t len) {/*...*/}
void the_cpp_wrapper(const std::vector<double>& v)
{
the_c_function(v.data(), v.size());
}
// ----------------------------
int main()
{
const std::vector<double> dbl_array { 3,4,5,6 };
the_cpp_wrapper(dbl_array);
return EXIT_SUCCESS;
}
关于const double*
你也应该更清楚与 double*
,C++ 故意希望您使用更冗长的 const_cast<double*>
丢弃const
-性。
如果你想“全力以赴”使用C++,你可以制作the_cpp_wrapper()
使用模板更通用一些:
template<typename TSpan>
void the_cpp_wrapper(const TSpan& v)
{
the_c_function(v.data(), v.size());
}
使用此代码,您可以将任何内容传递给 the_cpp_wrapper
有 data()
和 size()
方法。 (请注意 TSpan
“可以”是 std::span<int>
,这可能会导致一些模糊的编译器错误;有一些方法可以解决这个问题,但更多的是 C++。)
没有直接关系,但您可能会找到 std::span
也很有用。
关于c++ - 将引用用作数组/指针是否合法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66988477/
(注意:这个问题的动机是试图提出预处理器黑客来生成无操作分配来回答另一个问题: Macro that accept new object ...请记住这一点!) 这是一个人为的类: class foo
所以我尝试用java编写一个井字棋游戏。大部分已经完成,但是,如果有人选择了已被占用的空间,我无法返回无效的移动。 这是我试图弄清楚的代码。我认为由于空间是由数字 0 表示的(我的教授告诉我们这一点)
我想使用 Java 代码获取某些 Google 搜索引擎查询(在整个网络上)的估计结果数。 我每天只需要做很少的查询,所以一开始 Google Web Search API ,虽然已被弃用,但似乎已经
C++ Primer, 5e 的练习 4.20 询问表达式 iter++->empty(); 是否成立是合法的。假设iter是 vector::iterator . 这个表达式是合法的。我用gcc编译
C++ Primer, 5e 的练习 4.20 询问表达式 iter++->empty(); 是否成立是合法的。假设iter是 vector::iterator . 这个表达式是合法的。我用gcc编译
在 C++ 中,动态内存(取消)分配(malloc-free/new-delete)显然可以重复获得相同的内存范围,该内存范围被释放并按顺序再次分配。在多线程 C++ 中,这可能发生在多个线程中。 显
AesCryptoServiceProvider.LegalKeySizes 字段显示允许的位大小。 但是我不明白的是,如果这些都是真的,我如何才能成功利用 2048 位 key 长度(256 字节)
我有一个结构: typedef struct _n { int type; union { char *s; int i; }; } n; 当我
这个问题在这里已经有了答案: 关闭10年前. Possible Duplicate: int var = 1; void main() { int i = i; } 以下代码在g++和Visual C
根据 C++03 标准 (5.3.4/7): When the value of the expression in a direct-new-declarator is zero, the allo
我正在尝试创建一个生成器,使用 scalacheck 1.6.6 和规范 1.7 (scala 2.8.1) 生成(非零长度)合法的 unicode 字符串。 我希望我可以创建像这样的生成器: obj
这确实是一个关于 C++ 的问题,而不是 OpenGL。我正在关注 this OpenGL 教程(我刚开始),作者使用 C++(不是 C)。我的问题是 glVertexAttribPointer ta
由于 C++ 别名规则,您不能随意将 (一个 int*) 指向 char 数组,这似乎达成了一些共识。 从另一个问题——Generic char[] based storage and avoidin
我正在寻找一个通过 HTTP 接口(interface)发送文本消息的 SMS 网关。 但是,有些短信网关不允许设置发件人ID,允许的要么要求用户证明发件人ID是自己的手机号码,要么手动检查每个发件人
我们注意到您的应用要求用户注册个人信息才能访问非基于帐户的功能。在允许访问与用户无关的应用内容和功能之前,应用不能要求用户注册。 我们在注册时采用电子邮件 ID,因为所有数据都代表电子邮件 ID 在后
我一直在阅读 Bloch 和 Gafter 的 Java Puzzlers,并读到了第 10 个谜题 (Tweedledee)。这个谜题的本质是 provide declarations for th
这个问题确实符合标题:我很想知道造成这种差异的技术原因是什么,以及基本原理? std::shared_ptr sharedToVoid; // legal; std::unique_ptr uniqu
我试图做的是将数据框数据附加到现有的合法 excel 文件中。我使用了 openpyxl 中的 load_workbook() 函数,但它系统地返回了一个错误。这是一些在我的机器上崩溃的代码: fro
App 多次被 Apple 拒绝 “经过进一步审查,我们仍然发现您的应用使用了后台定位服务,但并未按照 iOS 人机界面指南的要求在位置模式警报中阐明其使用目的。” 在“NSLocationAlway
据我所知,像这样在 C++ 中实例化一个整数是合法的: int x = int(5); 作为一名 Java 程序员,我会假设这行代码调用传递“5”作为参数的整数的构造函数。我读到 int 不是一个类,
我是一名优秀的程序员,十分优秀!