Home Man
Search
Today's Posts
Register

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

Array initialization inside class in C++

Tags
array, c++, initialize, programming

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 02-02-2018
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?
# 2  
Old 02-02-2018
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};

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

# 4  
Old 02-02-2018
It's a warning in my compiler, and I'm not sure it actually does what you're expecting.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Array inside sed saleheen UNIX for Dummies Questions & Answers 6 01-30-2015 12:00 AM
How to use variable inside array? Jayavinoth Shell Programming and Scripting 2 01-13-2013 07:12 AM
static use for class inside the same class c++ xavipoes Programming 5 08-09-2012 07:54 PM
unique inside array anil510 Shell Programming and Scripting 4 05-11-2012 05:03 AM
Calling array inside awk drbiloukos Shell Programming and Scripting 3 02-07-2012 08:18 AM
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
using array inside awk subin_bala Shell Programming and Scripting 12 08-08-2008 04:38 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 02:23 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password