const array?

This is a discussion on const array? within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I'm trying to place an array of "unsigned int" in a class, but I don't seem to get ...

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    const array?

    Hi everyone, I'm trying to place an array of "unsigned int" in a class, but I don't seem to get it right. I know that in a class you can't initialize variables unless you do it inside a function, or else you can initialize them where you define them, as long as they are "const static"...

    So I try to place my "unsigned int" array in the class's declarations, but I get so many errors. This is what it looks like in the class (not inside any function):

    Code:
    class foo {
        private:
            const static unsigned int Array[10] = {12, 456, 89, 465, 7, 4, 64, 87, 65, 123};
    };
    (The array's values aren't really these here...) Sometime I get an error telling me that I can't initialize variables, unless they're "const static", sometimes it tells me that I have a problem with the "{", as if it was unexpected... And worst, if I try to use the array, I ge errors telling that my Array is not matched to any type, even though it's supposed to be "const static unsigned int"....

    Is it possible that we can't build constants arrays?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    instead of initialising the array there
    try,
    it after the semi colon of the class decl.
    Code:
    };//class over
    const unsigned int foo::Array[10] = {12, 456, 89};
    P.S.
    I'm not sure of the syntax, but initialisation defenitly comes outside the class declarn.
    other wise the compiler automaticaly inits it when the first object of foo is created filling Array with zeros.

  4. #4
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    Constants must also always be defined outside of the class they're declared in.
    Code:
    class foo {
        private:
            static const unsigned int Array[10];
    };
    
    const unsigned int foo::Array[10] = {12, 456, 89, 465, 7, 4, 64, 87, 65, 123};

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Put the array definition in a source file to avoid multiple definition errors.

  6. #6
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Well, I tried a few things, but even if I wrote the variable's value after the class, I wouldn't work. So I don't really know what to do with this, but I found another solution, however that solution doesn't really respect object-oriented rules: I just use a pointer in the class to the constants array, but the array is placed on global scope, not in the class...

    But that made me think about something else... So I figured out we can put pointers on constants, so because of this we can change the value of a constant variable. But then, what does being a constant really change if they can be changed with this "trick"... I though they were read-only or something, or that they were different...

  7. #7
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    Quote Originally Posted by mikahell
    But that made me think about something else... So I figured out we can put pointers on constants, so because of this we can change the value of a constant variable. But then, what does being a constant really change if they can be changed with this "trick"... I though they were read-only or something, or that they were different...
    I am just learning C++, but I tested this out and could only create a const pointer to a constant value. I couldn't create just a regular pointer to a constant value. Here's my code:

    Code:
    #include <iostream>
    
    
    int main()
    {
        const int num = 5;
        const int *p = &num; //coutln't be int *p = &num
        
        std::cout<<"num: "<<num<<std::endl;
        std::cout<<"*p: "<<*p<<std::endl;
        
        *p = 3; //this gives error
        std::cout<<"num: "<<num<<std::endl;
        std::cout<<"*p: "<<*p<<std::endl;   
        
        std::cin.ignore();
        std::cin.get();   
        
    }
    Is this what you are talking about?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Constants must also always be defined outside of the class they're declared in.
    You might want to check that statement for correctness. You're wrong on several counts.

    >But then, what does being a constant really change if they can be changed with this "trick"...
    A constant is read-only. It may or may not be in read-only memory. If you circumvent the type system to change the constness of a variable, you've invoked undefined behavior.
    My best code is written with the delete key.

  9. #9
    System.out.println("");
    Join Date
    Jan 2005
    Posts
    84
    http://books.google.com/books?vid=IS...lIM2pOCIJm3lf0

    I knew I read that in my book just the other day. It starts on this page and continues on 269.

  10. #10
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,438
    Daved gave you the answer.

    foo.h
    Code:
    class foo {
        private:
            const static unsigned int Array[10];
    };
    foo.cpp
    Code:
    #include "foo.h"
    const unsigned int foo::Array[10] = {12, 456, 89, 465, 7, 4, 64, 87, 65, 123};
    With or without const, it will work.

    Or... if you didn't want to define it static, you can initialize it inside the class constructor
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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