由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 如何 initialize array member?
相关主题
c++ initialize structc++标准函数传递一问
C++ questiontemplate 类的继承问题
C++ question急问:compile and build dependency
问一个 copy constructor 的问题 (C++)C++ Q 103-105 (转载)
关于C/C++里的Static variable的memory allocation/initializaC++ 问题
问个copy constructor的问题C++ 中 myobject * a =new myobject[n] 的问题
static initialization dependency c++question about const reference
a question about CASTC++弱问
相关话题的讨论汇总
话题: float话题: numbers话题: compile话题: int话题: arr
进入Programming版参与讨论
1 (共1页)
r****t
发帖数: 10904
1
这个应该是常见问题了,google 了一下确没有找到,这段小程序不能 compile:
template
class A {
public:
A(float numbers[N]):
numbers(numbers) {}
float numbers[N]; // this does not compile, see error msg below.
//float* numbers; // this works fine.
};
float arr[3] = {1.,2.,3.};
int main() {
A<3> a(arr);
}
compile 错误是:
test3.cpp: In constructor 'A::A(float*) [with int N = 3]':
test3.cpp:14: instantiated from here
test3.cpp:5: error: incompatible types in assignment
x****u
发帖数: 44466
2
数组不是C++的类型,放在参数里面就等于const pointer,你用union包装一下吧。

below.

【在 r****t 的大作中提到】
: 这个应该是常见问题了,google 了一下确没有找到,这段小程序不能 compile:
: template
: class A {
: public:
: A(float numbers[N]):
: numbers(numbers) {}
: float numbers[N]; // this does not compile, see error msg below.
: //float* numbers; // this works fine.
: };
: float arr[3] = {1.,2.,3.};

r****t
发帖数: 10904
3
用union包装是啥意思呢?展开说说?

【在 x****u 的大作中提到】
: 数组不是C++的类型,放在参数里面就等于const pointer,你用union包装一下吧。
:
: below.

p***o
发帖数: 1252
4
memcpy or std::copy

below.

【在 r****t 的大作中提到】
: 这个应该是常见问题了,google 了一下确没有找到,这段小程序不能 compile:
: template
: class A {
: public:
: A(float numbers[N]):
: numbers(numbers) {}
: float numbers[N]; // this does not compile, see error msg below.
: //float* numbers; // this works fine.
: };
: float arr[3] = {1.,2.,3.};

r****t
发帖数: 10904
5
我觉得在 ctor 里面 memcpy 应该是个好办法。

【在 p***o 的大作中提到】
: memcpy or std::copy
:
: below.

t****t
发帖数: 6806
6
first of all, you can not copy array by using numbers(numbers). must use
memcpy or std::copy.
second, if you declare ctor as A::A(float numbers[N]), it is equivalent to A
float[1] without compiling errors, which may not be desirable. depending on
your requirement, you may want to use
A::A(const float (&numbers)[N])
as ctor, which preserves length information and enforces that you can only
send in float[N] at compile time.
below.
r****t
发帖数: 10904
7

A
on
对,这个是唯一用一个 argument 还能 enforce N 的办法

【在 t****t 的大作中提到】
: first of all, you can not copy array by using numbers(numbers). must use
: memcpy or std::copy.
: second, if you declare ctor as A::A(float numbers[N]), it is equivalent to A
: float[1] without compiling errors, which may not be desirable. depending on
: your requirement, you may want to use
: A::A(const float (&numbers)[N])
: as ctor, which preserves length information and enforces that you can only
: send in float[N] at compile time.
: below.

1 (共1页)
进入Programming版参与讨论
相关主题
C++弱问关于C/C++里的Static variable的memory allocation/initializa
不明白C++的一个地方问个copy constructor的问题
C++ (direct vs indirect initialization)static initialization dependency c++
Initialization list的一个问题a question about CAST
c++ initialize structc++标准函数传递一问
C++ questiontemplate 类的继承问题
C++ question急问:compile and build dependency
问一个 copy constructor 的问题 (C++)C++ Q 103-105 (转载)
相关话题的讨论汇总
话题: float话题: numbers话题: compile话题: int话题: arr