我正在为linux和windows编写一个应用程序,并注意到GCC构建产生了许多对复制构造函数的无用调用.
以下是产生此行为的示例代码:
struct A
{
A() { std::cout << "default" << std::endl; }
A(A&& rvalue) { std::cout << "move" << std::endl; }
A(const A& lvalue) { std::cout << "copy" << std::endl; }
A& operator =(A a) { std::cout << "assign" << std::endl; return *this; }
};
BOOST_AUTO_TEST_CASE(test_copy_semantics)
{
std::vector<A> vec_a( 3 );
}
Run Code Online (Sandbox Code Playgroud)
此测试仅创建3个元素的向量.我期望3个默认构造函数调用和0个副本,因为没有A左值.
在Visual C++ 2010中,输出为:
default
move
default
move
default
move
Run Code Online (Sandbox Code Playgroud)
在GCC 4.4.0(MinGW)中,( - 02 -std = c ++ 0x),输出为:
default
copy
copy
copy
Run Code Online (Sandbox Code Playgroud)
发生了什么,我该如何解决?副本对于实际类来说是昂贵的,默认构造和移动都很便宜.
如何返回存储在a中的多维数组 private我的类字段中?
class Myclass {
private:
int myarray[5][5];
public:
int **get_array();
};
// This does not work:
int **Myclass::get_array() {
return myarray;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
无法转换
int (*)[5][5]为int**回报
我是一个初学程序员并且对C知之甚少.我知道的一件事但是没有向我解释过为什么函数不能返回数组.通过使用指针或将数组包装在a中,可以很容易地避免这种情况(从函数返回一个2d数组)struct,但我的问题是"为什么?".
我知道有一些简单的解决方法,但自从我发现它之后,这个问题一直困扰着我.这是C内部设计的一个缺陷,还是故意将它放在那里?是否存在这种限制的特殊原因?
我没有看到任何合乎逻辑的理由.我的意思是你可以通过使用包含这样的数组成员的结构轻松克服这个要求:
template <size_t n>
struct arr { int d[n]; };
auto fnReturningArray()
{
return arr<3>{0, 1, 2};
};
Run Code Online (Sandbox Code Playgroud)
其行为方式与直接返回数组的方式完全相同,只是您应首先访问结构成员'd'以使用它.标准本身也通过'std :: array'类型添加了类似的功能.所以它似乎可以实现.为什么ISO C++禁止这个动作?也许遗留代码兼容性(但我很难相信这是因为添加的其他新东西已经很久了,例如'auto'关键字的新含义).
我想检查一个类型是否在std :: numeric_limits中有一个条目.当类型是数组 - (或者可能不是数字?)时,我得到编译器错误.这使我无法根据std :: numeric_limits中是否支持该类型来检测和分支.我很感激任何想要分享的见解.
// the following provokes compiler error on Clang
// Function cannot return array type 'type' (aka 'char [20]')
static_assert(
! std::numeric_limits<char[20]>::is_specialized,
"! std::numeric_limits<char[20]>::is_specialized"
);
// invokes static assert on compile as expected
static_assert(
std::numeric_limits<char[20]>::is_specialized,
"std::numeric_limits<char[20]>::is_specialized"
);
Run Code Online (Sandbox Code Playgroud) 为什么这段代码需要数组语法中的'&'?
int (&returnArray(int (&arr)[42]))[42]
{
return arr;
}
Run Code Online (Sandbox Code Playgroud)
当我宣布这样的时候
int (returnArray(int arr[42]))[42]
{
return arr;
}
Run Code Online (Sandbox Code Playgroud)
我明白了
error C2090: function returns array
Run Code Online (Sandbox Code Playgroud)
但这不是第一个例子中返回的数组吗?它是某种对数组的引用吗?
我知道我也可以将一个数组传递给一个函数,它会衰减到一个指针
int returnInt(int arr[42])
{
return arr[0];
}
Run Code Online (Sandbox Code Playgroud)
或通过引用传递它
int returnInt(int (&arr)[42])
{
return arr[0];
}
Run Code Online (Sandbox Code Playgroud)
但为什么我不能以相同的方式返回数组?
我有一个声明如下的函数:
unsigned char** Classifier::classify(){
//...
unsigned char **chars = new unsigned char *[H];
for(int i = 0; i < H; i++)
chars[i] = new unsigned char[W*3];
//...
return &chars;
//note: when this is "return chars;" I get the following: cannot convert ‘unsigned char*’ to ‘unsigned char**’ in return
Run Code Online (Sandbox Code Playgroud)
这给了我警告:
Classifier.cpp: In member function ‘unsigned char** Classifier::classify()’:
Classifier.cpp:124: warning: address of local variable ‘chars’ returned
Run Code Online (Sandbox Code Playgroud)
这可以忽略吗?基本上,我的问题是如何返回对函数中定义的数组的引用?
我希望能够做到
unsigned char** someData = classify();
Run Code Online (Sandbox Code Playgroud) 一个问题设置 ++的人学习C是
写一个简短的程序来模拟从塔上掉下来的球.首先,应该要求用户以米为单位的塔的初始高度.假设正常重力(9.8 m/s2),并且球没有初始速度.让程序在0,1,2,3,4和5秒后输出球高于地面的高度.球不应该在地下(高度0).
在开始使用C++之前,我有一个合理的,但主要是自学成才的Java知识.所以看看这个问题似乎应该分成两部分
输入类将询问用户的起始高度,该起始高度将传递给控制器.控制器会给计算类提供这个和几秒钟(5),这将创建一个结果数组并将其返回给控制器.控制器会将结果数组交给输出类,然后将它们打印到控制台.
我将实际代码放在底部,但可能不需要.
您可能已经看到问题,尝试返回一个数组.我不问如何避开这个问题,有解决方法在这里和这里.我问,问题是设计糟糕的结果吗?我的程序是否应该以不同的方式构建,出于性能,维护或样式的原因,我不会尝试返回像对象一样的数组?
这是代码(与尝试返回数组不同);
main.cpp中
/*
* Just the main class, call other classes and passes variables around
*/
#include <iostream>
#include "dropSim.h"
using namespace std;
int main()
{
double height = getHeight();
int seconds = 5;
double* results = calculateResults(height, seconds);
outputResults(results);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
getHeight.cpp
/*
* Asks the user for a height from which …Run Code Online (Sandbox Code Playgroud) 我是C语言的新手,我想知道:
为什么C函数不能返回数组类型?
我知道数组名称是数组第一个值的地址,而数组是C语言中的二等公民.
为什么这在C中有效
int * foo(int a,int b){
...
}
Run Code Online (Sandbox Code Playgroud)
但这是无效的
int [] foo(int a,int b){
...
}
Run Code Online (Sandbox Code Playgroud) 我一直认为,当你想从一个函数返回一个数组时,唯一的方法是使用像这样的指针:
char * func();
但昨天,当我经历K&R时,我注意到 错误地认为这char x()[]也是一个有效的结构.所以我继续测试了这个并编写了以下代码:
#include <stdio.h>
#include <stdlib.h>
char string1[10] = "123456789";
char x(void)[10];
int main(void) {
printf("string returned by x() is %s",x());
return EXIT_SUCCESS;
}
char x(void)[10] {
return x;
}
Run Code Online (Sandbox Code Playgroud)
在Windows上使用GCC进行编译时,会产生以下错误:
..\src\07arrreturn.c:7:6: error: 'x' declared as function returning an array
..\src\07arrreturn.c: In function 'main':
..\src\07arrreturn.c:10:2: warning: format '%s' expects argument of type 'char *', but argument 2 has type 'int' [-Wformat]
..\src\07arrreturn.c: At top level:
..\src\07arrreturn.c:14:6: error: 'x' declared as function …Run Code Online (Sandbox Code Playgroud)