成员数组的默认参数? [英] Default parameter for member array?

查看:76
本文介绍了成员数组的默认参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在这个例子中,没有办法为构造函数A :: A(char(& array)[16])提供

参数的默认值。

正确吗?


struct A {

A(char(& array)[16]){

std :: copy(& array [0],& array [16],& _array [0]);

}

char _array [16];

};


int main(){

char arr [16] = {0};

A a(arr);

std :: cout<< a._array<< std :: endl;

}


-

如果我们的假设是关于任何事情而不是关于某一个或多个特定事物,那么我们的推论构成数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell

解决方案

* Steven T. Hatton:

据我所知,没有办法为<提供默认值在这个例子中,构造函数A :: A(char(& array)[16])的参数。
正确吗?



char z [16] = {};

struct A {
A(char(& array)[16 ]){


A(char(& array)[16] = z)


std :: copy(& array [ 0],&安培;阵列[16],&安培; _array [0]);


你知道,只有下划线不同的名字在视觉上并不明显。


}
char _array [16];
};

int main(){
char arr [16] = {0};
A a(arr) ;
std :: cout<< a._array<< std :: endl;
}




int main(){

A a;

std :: cout<< a._array<< std :: endl;

}


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的是什么?


Alf P. Steinbach写道:

* Steven T. Hatton:

据我所知,没有办法提供默认值对于构造函数A :: A(char(& array)[16])的
参数,在这个例子中。
正确吗?



char z [16] = {};




太糟糕了我不能把它作为在POD类上执行的默认操作

当它被构造时。 AFAIK,我可以这样做。


struct C {int _a; int _b; };


C c = {};


可靠地零初始化POD类的所有成员。但我没有看到

一种方法来使用它作为默认构造函数。在初始化列表中写出所有成员

可能很乏味。


struct A {
A(char(& array)[16]){



A(char(& array)[16] = z)

< blockquote class =post_quotes> std :: copy(& array [0],& array [16],& _array [0]);



你知道,名字只有下划线不同的东西在视觉上并不鲜明。




实际上,我很邋。通常我会这样做:


struct A {

A(char(& array _)[16])

{ std :: copy(& array_ [0],& array [16] _,& _array [0]);}

char _array [16];

};


在这种情况下,由于语法太忙,所以没有多大帮助。然而,在

general中,由于我经常使用_member作为成员

声明,而member_作为参数,我很有条件寻找它们。

它运作得相当好。唯一真正引起我很多问题的符号

是''#''。

}
char _array [16];
};

int main(){
char arr [16] = {0};
A a(arr);
std :: cout<< a._array<< std :: endl;
}



int main(){
A a;
std :: cout<< a._array<< std :: endl;
}




有趣的是,如果参数不是const对于z,它不会接受const char []

,但据我所知,z和A :: _数组是两个不同的对象,

so z永远不会因初始化而被修改。通常

无论如何,构造函数的参数应该是const。我也发现我可以将默认的初始化程序设为静态成员,这可以满足我对正确收容的要求。


这段代码显示了一些从查看

语法不明显的行为。


#include< iostream>

# include< string>

std :: string s(" surprise!");


A类{

static const char s_Z [16];

public:

A(const char(& array _)[16] = s_Z){

std :: copy(& array_ [0],& array_ [16],& _array [0]);

}

void surprise(){std ::拷贝(安培; S [0],&安培; S [s.size()],&安培; _array [0]); } b / b $ _ $ $ [$]
};

const char A :: s_Z [16] = 1 '', '' 2 '', '' 3 '', '' 4 '', '' 5 '', '' 6 '', '' 7 '', '' 8 '', '' 9' '

,'''',''B'',''C'','D'',''E'',''F'',''\ 0''};


char z [16] = {};


struct B {

B (const char(& array _)[16] = z){

std :: copy(& array_ [0],& array_ [16],& _array [0]);

}

char _array [16];

};


int main(){

char arr [16] = {''1'',''2'',''3'',''4'',''5'',''6'' ,''7'',''8'',''9''

,'''',''b'',''c'',''d'' ,''e'',''f'',''\ 0''};

A a(arr);

A aa;

std :: cout<< a._array<< std :: endl;

a.surprise();

std :: cout<< aa._array<< std :: endl;

std :: cout<< a._array<< std :: endl;

std :: cout<< z<< std :: endl;

B b;

std :: copy(& s [0],& ; s [s.size()],& z [0]);

B bb;

std :: cout<< b._array<< std :: endl;

std :: cout<< bb._array<< std :: endl;

}


-

如果我们的假设是关于任何事情而不是关于某一个或多个特定事物,那么我们的推论就构成了数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell


* Steven T. Hatton:

[someething]







-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这是一件坏事?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?

As far as I know, there is no way to provide a default value for the
argument to the constructor A::A(char (&array)[16]) in this example.
Correct?

struct A{
A(char (&array)[16] ){
std::copy(&array[0],&array[16],&_array[0]);
}
char _array[16];
};

int main() {
char arr[16] = {0};
A a(arr);
std::cout<<a._array<<std::endl;
}

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell

解决方案

* Steven T. Hatton:

As far as I know, there is no way to provide a default value for the
argument to the constructor A::A(char (&array)[16]) in this example.
Correct?

char z[16] = {};

struct A{
A(char (&array)[16] ){
A( char (&array)[16] = z )

std::copy(&array[0],&array[16],&_array[0]);
You know, names that differ only in an underscore are not visually
distinctive.

}
char _array[16];
};

int main() {
char arr[16] = {0};
A a(arr);
std::cout<<a._array<<std::endl;
}



int main() {
A a;
std::cout<<a._array<<std::endl;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Alf P. Steinbach wrote:

* Steven T. Hatton:

As far as I know, there is no way to provide a default value for the
argument to the constructor A::A(char (&array)[16]) in this example.
Correct?


char z[16] = {};



Too bad I can''t have that as the default operation performed on a POD class
when it''s constructed. AFAIK, I can do this.

struct C { int _a; int _b; };

C c = {};

To reliably zero-initialize all the members of a POD class. But I don''t see
a way to use that for the default constructor. Writing out all the members
in the initialization list can be tedious.


struct A{
A(char (&array)[16] ){



A( char (&array)[16] = z )

std::copy(&array[0],&array[16],&_array[0]);



You know, names that differ only in an underscore are not visually
distinctive.



Actually, I was being sloppy. Typically I will do this:

struct A{
A(char (&array_)[16] )
{std::copy(&array_[0],&array[16]_,&_array[0]);}
char _array[16];
};

Which in this case doesn''t help a lot because the syntax is so busy. In
general, however, since I frequently use _member for the member
declaration, and member_ for a parameter, I''m conditioned to look for them.
It works fairly well. The only symbol that really causes me much problem
is ''#''.

}
char _array[16];
};

int main() {
char arr[16] = {0};
A a(arr);
std::cout<<a._array<<std::endl;
}



int main() {
A a;
std::cout<<a._array<<std::endl;
}



Interestingly, if the parameter is not const it won''t accept a const char[]
for z, but as far as I can tell, z and A::_array are two distinct objects,
so z would never be modified as a result of that initialization. Typically
the parameter to the constructor should probably be const anyway. I also
found that I can make the default initializer a static member, which
satisfies my desire for proper containment.

This code show some behavior that was not obvious from looking at the
syntax.

#include <iostream>
#include <string>
std::string s("surprise!");

class A {
static const char s_Z[16];
public:
A(const char (&array_)[16] = s_Z ) {
std::copy(&array_[0],&array_[16],&_array[0]);
}
void surprise() { std::copy(&s[0],&s[s.size()],&_array[0]); }
char _array[16];
};

const char A::s_Z[16] = {''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9''
,''A'',''B'',''C'',''D'',''E'',''F'',''\0''};

char z[16] = {};

struct B {
B( const char (&array_)[16] = z ) {
std::copy(&array_[0],&array_[16],&_array[0]);
}
char _array[16];
};

int main() {
char arr[16] = {''1'',''2'',''3'',''4'',''5'',''6'',''7'',''8'',''9''
,''a'',''b'',''c'',''d'',''e'',''f'',''\0''};
A a(arr);
A aa;
std::cout<<a._array<<std::endl;
a.surprise();
std::cout<<aa._array<<std::endl;
std::cout<<a._array<<std::endl;
std::cout<<z<<std::endl;
B b;
std::copy(&s[0],&s[s.size()],&z[0]);
B bb;
std::cout<<b._array<<std::endl;
std::cout<<bb._array<<std::endl;
}

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell


* Steven T. Hatton:

[someething]



?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于成员数组的默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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