40886

initializing a static const array inside a class - c++

<h3>Question</h3>

Consider the following code, I've marked the important line with #this symbol:

#include <glad/include/glad/glad.h> #include <string> #include <iostream> #ifndef LAMP_H #define LAMP_H namespace lmp{ class genLamp{ unsigned int lmpVAO; static const float flag{1}; //#this is allowed static const float default_shape[]{ //#this is not allowed -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 1.0f }; genLamp(std::string vShaderPath, std::string fShaderPath){ glGenVertexArrays(1, &lmpVAO); glBindVertexArray(lmpVAO); } unsigned int getVAO(){ return this->lmpVAO; } }; } #endif

<em>First of all</em>, why is this even not allowed, what problem is the language trying to prevent by preventing this? And,

Since the default_shape array is going to be the same across objects no matter what, I was trying to share this array by making it static. But, this doesn't seem to be possible. The only thing I can think of is declaring the variable into a global scope, which is not so good in my case. Does c++ have any syntax to declare and initialize static const arrays ? I'm compiling with c++17 in case the information is useful.

EDIT: if possible please also explain @user's answer


<h3>Answer1:</h3>

Make them inline. The following code compiles.

class Temp { inline static const float values[] = { 0.0f, 1.0f }; };

Or even better,

class Temp { constexpr static float values[] = { 0.0f, 1.0f }; };

Thanks to John for pointing this out.

来源:https://stackoverflow.com/questions/62242578/initializing-a-static-const-array-inside-a-class-c

Recommend

  • Sequentially run commands in bash script
  • “Read-Only” Entity Framework? I'm trying to use RIA Services, EF, and Silverlight
  • Viewport for ipad portrait [only]
  • How to retrieve data from Firebase Database with join if key only return true?
  • how to animate two continuous popup windows with jquery mobile?
  • Open default mail app from within Qt with some html
  • How would I make a polygon based on the relative positions of markers in google maps?
  • Getting nil from standardUserDefaults,
  • How to write a method signature “T that implements Comparable” in Java?
  • Debug Azure Functions locally
  • Zip library for Windows Phone 7
  • Django IN query as a string result - invalid literal for int() with base 10
  • Keyboard Extension Crash on Device
  • google fusion table- not able to color the layer in map after 5 colors
  • Difference between assigning instantiation to parent class and derived class
  • Heroku Git Push Master Error
  • How to HTTP POST a Blob to a different domain? [duplicate]
  • Faces Servlet not parsing .xhtml pages in jsf 2. running on tomcat 7
  • jQuery how to translate livequery to on?
  • Firebase suddenly reports invalid signature
  • Using django-multiupload within a ModelForm
  • Threads and Concurrent Modification Exception working with a list
  • jQuery Ajax call to WCF service returning “Method not allowed (405)”
  • Extracting a small subset of data from XMLs
  • Boolean filter using a timestamp value on a dataframe in Python
  • css: column-count 3, image floating spanning 2, chrome not playing. why?
  • Symfony - Setting Cookie onKernelRequest
  • Haskell program that can handle any arbitrary deterministic finite automaton
  • Fortran function variable length string return
  • Add checkbox dynamically using angular 2
  • Controller or RestController
  • Year over Year Stats from a Crossfilter Dataset
  • Ruby regex for matching simpliest Ruby's regexes
  • Why is ordered choice in pyparsing failing for my use case?
  • Calculate time from document
  • How to turn off notice reporting in xampp?
  • Google App Engine Datastore: Dealing with eventual consistency
  • Sign a Pdf using custom digital signature in Java
  • Accessing Arguments, Workflow Variables from custom activities
  • ReferenceError: TextEncoder is not defined