const arrays in classes.

This is a discussion on const arrays in classes. within the C++ Programming forums, part of the General Programming Boards category; Code: class xyz { const int arr[] = { 1, 2, 3, 4, 5 }; }; Would I be correct ...

  1. #1
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

    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!

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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;
    }

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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.

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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.
    Last edited by The Dog; 04-18-2004 at 09:18 AM.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,676
    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

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    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.

  7. #7
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic arrays of const objects under VC++ 2005
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 06-04-2006, 03:34 PM
  2. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 02:46 AM
  3. "error: incomplete type is not allowed"
    By Fahrenheit in forum C++ Programming
    Replies: 9
    Last Post: 05-10-2005, 09:52 PM
  4. Memory leak - need help finding
    By ChadJohnson in forum C++ Programming
    Replies: 8
    Last Post: 04-06-2005, 07:26 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21