Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Initializing a Static Stack
    text
    copied!<p>I have some questions about initializing a static collection. Here is an example I coded that seems to work:</p> <pre><code>#include &lt;stack&gt; #include &lt;iostream&gt; using namespace std; class A { private: static stack&lt;int&gt; numbers; static stack&lt;int&gt; initializeNumbers(); public: A(); }; A::A() { cout &lt;&lt; numbers.top() &lt;&lt; endl; } stack&lt;int&gt; A::initializeNumbers() { stack&lt;int&gt; numbers; numbers.push(42); return numbers; } stack&lt;int&gt; A::numbers = initializeNumbers(); int main() { A a; } </code></pre> <p>Now, is this the best way to do what I'm trying to accomplish? For some reason, when I try this exact same scheme in my real code, calling <em>top()</em> prints gibberish. Could there be any reason for this?</p> <p>If my example is fine, perhaps I will resort to posting my real code.</p> <hr> <p>Here is the real code:</p> <p><em>Light.h</em></p> <pre><code>#ifndef LIGHT_H_ #define LIGHT_H_ #include &lt;stack&gt; #include "Vector4.h" class Light { private: static stack&lt;GLenum&gt; availableLights; static stack&lt;GLenum&gt; initializeAvailableLights(); public: GLenum lightID; Vector4 ambient, diffuse, specular, position, spotDirection; GLfloat constantAttenuation, linearAttenuation, quadraticAttenuation, spotExponent, spotCutoff; Light( const Vector4&amp; ambient = Vector4(0.0, 0.0, 0.0, 1.0), const Vector4&amp; diffuse = Vector4(1.0, 1.0, 1.0, 1.0), const Vector4&amp; specular = Vector4(1.0, 1.0, 1.0, 1.0), const Vector4&amp; position = Vector4(0.0, 0.0, 1.0, 0.0), const Vector4&amp; spotDirection = Vector4(0.0, 0.0, -1.0, 0.0), GLfloat constantAttenuation = 1.0, GLfloat linearAttenuation = 0.0, GLfloat quadraticAttenuation = 0.0, GLfloat spotExponent = 0.0, GLfloat spotCutoff = 180.0); ~Light(); }; #endif /*LIGHT_H_*/ </code></pre> <p><em>Light.cpp</em></p> <pre><code>#include &lt;stdexcept&gt; // runtime_error #include &lt;iostream&gt; using namespace std; #include "Light.h" Light::Light( const Vector4&amp; ambient, const Vector4&amp; diffuse, const Vector4&amp; specular, const Vector4&amp; position, const Vector4&amp; spotDirection, GLfloat constantAttenuation, GLfloat linearAttenuation, GLfloat quadraticAttenuation, GLfloat spotExponent, GLfloat spotCutoff) : ambient(ambient), diffuse(diffuse), specular(specular), position(position), spotDirection(spotDirection), constantAttenuation(constantAttenuation), linearAttenuation(linearAttenuation), quadraticAttenuation(quadraticAttenuation), spotExponent(spotExponent), spotCutoff(spotCutoff) { // This prints gibberish. cout &lt;&lt; availableLights.size() &lt;&lt; endl; // The error is indeed thrown. if(availableLights.empty()) throw runtime_error("The are no more available light identifiers."); else { lightID = availableLights.top(); availableLights.pop(); } } Light::~Light() { availableLights.push(this -&gt; lightID); } stack&lt;GLenum&gt; Light::initializeAvailableLights() { stack&lt;GLenum&gt; availableLights; availableLights.push(GL_LIGHT7); availableLights.push(GL_LIGHT6); availableLights.push(GL_LIGHT5); availableLights.push(GL_LIGHT4); availableLights.push(GL_LIGHT3); availableLights.push(GL_LIGHT2); availableLights.push(GL_LIGHT1); availableLights.push(GL_LIGHT0); return availableLights; } stack&lt;GLenum&gt; Light::availableLights = initializeAvailableLights(); </code></pre> <hr> <p>And since I can't get the code with the stack to work, I've opted for this at the moment:</p> <p><em>Light.h</em></p> <pre><code>#ifndef LIGHT_H_ #define LIGHT_H_ #include &lt;stack&gt; #include "Vector4.h" class Light { private: static const unsigned int LIGHTS = 9; static bool availableLights[]; static GLenum lights[]; static GLenum getAvailableLight(); public: GLenum lightID; Vector4 ambient, diffuse, specular, position, spotDirection; GLfloat constantAttenuation, linearAttenuation, quadraticAttenuation, spotExponent, spotCutoff; Light( const Vector4&amp; ambient = Vector4(0.0, 0.0, 0.0, 1.0), const Vector4&amp; diffuse = Vector4(1.0, 1.0, 1.0, 1.0), const Vector4&amp; specular = Vector4(1.0, 1.0, 1.0, 1.0), const Vector4&amp; position = Vector4(0.0, 0.0, 1.0, 0.0), const Vector4&amp; spotDirection = Vector4(0.0, 0.0, -1.0, 0.0), GLfloat constantAttenuation = 1.0, GLfloat linearAttenuation = 0.0, GLfloat quadraticAttenuation = 0.0, GLfloat spotExponent = 0.0, GLfloat spotCutoff = 180.0); ~Light(); }; #endif /*LIGHT_H_*/ </code></pre> <p><em>Light.cpp</em></p> <pre><code>#include &lt;stdexcept&gt; // runtime_error #include "Light.h" Light::Light( const Vector4&amp; ambient, const Vector4&amp; diffuse, const Vector4&amp; specular, const Vector4&amp; position, const Vector4&amp; spotDirection, GLfloat constantAttenuation, GLfloat linearAttenuation, GLfloat quadraticAttenuation, GLfloat spotExponent, GLfloat spotCutoff) : ambient(ambient), diffuse(diffuse), specular(specular), position(position), spotDirection(spotDirection), constantAttenuation(constantAttenuation), linearAttenuation(linearAttenuation), quadraticAttenuation(quadraticAttenuation), spotExponent(spotExponent), spotCutoff(spotCutoff) { lightID = getAvailableLight(); } Light::~Light() { for(unsigned int i = 0; i &lt; LIGHTS; i++) if(lights[i] == lightID) availableLights[i] = true; } bool Light::availableLights[] = {true, true, true, true, true, true, true, true}; GLenum Light::lights[] = {GL_LIGHT0, GL_LIGHT1, GL_LIGHT2, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7}; GLenum Light::getAvailableLight() { for(unsigned int i = 0; i &lt; LIGHTS; i++) if(availableLights[i]) { availableLights[i] = false; return lights[i]; } throw runtime_error("The are no more available light identifiers."); } </code></pre> <hr> <p>Can anyone spot an error in the code with the stack, or perhaps improve upon my hastily coded workaround?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload