Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Array initialization inside class in C++

Programming


Tags
array, c++, initialize

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 02-02-2018   -   Original Discussion by milhan
milhan's Unix or Linux Image
milhan milhan is offline
Registered User
 
Join Date: Oct 2002
Last Activity: 22 May 2018, 11:03 AM EDT
Location: New England, USA
Posts: 188
Thanks: 9
Thanked 4 Times in 4 Posts
Array initialization inside class in C++



Code:
const int VALUES[] = {7,4,2,1,0}; //or int VALUES[] = {7,4,2,1,0};

this statement inside a class definition gives error. Why?
Sponsored Links
    #2  
Old Unix and Linux 02-02-2018   -   Original Discussion by milhan
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 May 2018, 3:03 PM EDT
Location: Saskatchewan
Posts: 22,674
Thanks: 1,177
Thanked 4,321 Times in 3,984 Posts
Because they don't belong there. Also, you must make it static to define a constant value in a class at compile time. Then the compiler will allow you to define its values outside the class.



Code:
// This bit goes in header file
class asdf {
        static const int VALUES[];
};

// This bit should not go in a header file, because it should occur once and only once
const int asdf::VALUES[]={7,4,2,1,0};

Sponsored Links
    #3  
Old Unix and Linux 02-02-2018   -   Original Discussion by milhan
milhan's Unix or Linux Image
milhan milhan is offline
Registered User
 
Join Date: Oct 2002
Last Activity: 22 May 2018, 11:03 AM EDT
Location: New England, USA
Posts: 188
Thanks: 9
Thanked 4 Times in 4 Posts


Code:
// but this compiles and works fine
class asdf {
        const int VALUES[5] = {7,4,2,1,0};
};

    #4  
Old Unix and Linux 02-02-2018   -   Original Discussion by milhan
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 22 May 2018, 3:03 PM EDT
Location: Saskatchewan
Posts: 22,674
Thanks: 1,177
Thanked 4,321 Times in 3,984 Posts
It's a warning in my compiler, and I'm not sure it actually does what you're expecting.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
static use for class inside the same class c++ xavipoes Programming 5 08-09-2012 07:54 PM
Link array to class java guidely Programming 0 09-28-2011 08:08 AM
Class Pointer initialization C++ mind@work Programming 10 05-05-2011 12:42 AM
split and making an array inside another array dcfargo Shell Programming and Scripting 2 08-06-2008 10:07 AM
looping a array inside inside ssh is not working, pls help reldb Shell Programming and Scripting 5 07-07-2006 10:32 AM



All times are GMT -4. The time now is 03:25 AM.