array-initialize

Array initialization with default constructor

旧街凉风 提交于 2019-11-26 14:20:47
问题 public class Sample { static int count = 0; public int abc; public Sample() { abc = ++Sample.count; } } I want to create an array of above class, and want each element in the array to be initialized by invoking the default constructor, so that each element can have different abc .So I did this: Sample[] samples = new Sample[100]; But this doesn't do what I think it should do. It seems this way the default constructor is not getting called. How to invoke default constructor when creating an

initialize a const array in a class initializer in C++

百般思念 提交于 2019-11-26 11:20:41
I have the following class in C++: class a { const int b[2]; // other stuff follows // and here's the constructor a(void); } The question is, how do I initialize b in the initialization list, given that I can't initialize it inside the body of the function of the constructor, because b is const ? This doesn't work: a::a(void) : b([2,3]) { // other initialization stuff } Edit: The case in point is when I can have different values for b for different instances, but the values are known to be constant for the lifetime of the instance. Like the others said, ISO C++ doesn't support that. But you

How to initialize all members of an array to the same value?

随声附和 提交于 2019-11-25 21:39:41
问题 I have a large array in C (not C++ if that makes a difference). I want to initialize all members to the same value. I could swear I once knew a simple way to do this. I could use memset() in my case, but isn\'t there a way to do this that is built right into the C syntax? 回答1: Unless that value is 0 (in which case you can omit some part of the initializer and the corresponding elements will be initialized to 0), there's no easy way. Don't overlook the obvious solution, though: int myArray[10]