-
const arrays in classes.
Code:
class xyz
{
const int arr[] = { 1, 2, 3, 4, 5 };
};
Would I be correct in saying that there is no way to initialize a const array in a class(equivalent to above)? You can't initalize arrays in the member initialization list and you can only initialize static const inttype in the class body.
It seems the closest you can get is to lose the const and initialize the array manually:
Code:
class xyz
{
int arr[5];
public:
xyz()
{
arr[0] = 1;
arr[1] = 2;
...
}
};
This seems messy in the extreme. Any ideas?
Similarly, there seems be no way to initialize static const members, which are not integral types:
Code:
class xyz
{
static const double d; // no way to initialize!
}
Of course, this can go outside the class without too much negative effect, but this is hardly clean.
Is it just me, or is C++ very messy and overly complex? There seems to be three places you can initialize member variables(depending on their types!) and still, as far as I can tell, there are variable types that can not be cleanly initialized!
-
I'm only aware of how to initialise static members. Here's an example.
Code:
#include <iostream>
class intObject
{
public:
int value;
intObject(int x=5):value(x){}
};
class test
{
public:
test(){}
static intObject object;
};
// Initialisation
intObject test::object = intObject(10);
class xyz
{
public:
xyz(){}
static const int arr[];
};
// Initialisation
const int xyz::arr[] = { 1, 2, 3, 4, 5 };
int main(void)
{
std::cout << xyz::arr[0] << xyz::arr[1] << xyz::arr[2] << xyz::arr[3] << xyz::arr[4] << std::endl;
std::cin.get();
return 0;
}
-
Thanks, that is far better than declaring all my non-integral static consts outside the class.
Does anyone know why when I try:
Code:
class intObject
{
public:
int value;
intObject(int x=5):value(x){}
};
class test
{
private:
static intObject object;
public:
test(){}
};
// Initialisation
intObject test::object = intObject(10); /* No error!!?? */
class xyz
{
private:
static const int arr[];
public:
xyz(){}
};
// Initialisation
const int xyz::arr[] = { 1, 2, 3, 4, 5 }; /* Error */
On MSVC.
-
It's private in your code and as such is not accessible.
//Sorry, I just compiled it using BCC32 and got no errors.
I'm not sure why it doesn't work in MSVC.
-
My MSVC 6.0 compiles it fine.
>> It seems the closest you can get is to lose the const and initialize the array manually:
Yep. Or with a memcpy() or whatever.
It doesn't make sense to have a const array as a class/struct member since there's no need to duplicate that data (because it's constant).
gg
-
You can't declare an array in a class like this because you're trying to allocate memory in a class definition
Code:
class xyz
{
const int arr[] = { 1, 2, 3, 4, 5 };
};
You might be able to do this
Code:
class xyz
{
private:
int * const arr;
public:
xyz();
};
xyz::xyz()
:arr(new int[5])
{
//assign all values here
}
you can only initialize const class members in the constructor using the initializer list. The above code will work if you want the pointer to be constant. If you want the values of the array to be constant, the only thing I can see doing is using const_cast, but I'm sure there are better ways of getting the same result.
-
>> It doesn't make sense to have a const array as a class/struct member since there's no need to duplicate that data (because it's constant <<
I agree, it would be rare, but an example where you may want this is using a class as a network or file protocol packet. I guess in this situation a memcpy() from a static const would be fine.
My interest was primarily in encapsulation. For this, the Dog's method seems to do the trick fairly nicely.
Skorman, thanks for the info. Your method adds to the mix.
I'm just starting on my first non-trivial C++ code so you can expect lots more questions and unwarranted complaining.