Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat do getters and setters look like for a struct (that lives оn the heap)?
    text
    copied!<p>Very simple question - how can I access data for a struct that lives inside the heap?</p> <p>Additional information:: I have a class called "A" and a struct within this class called "B". Every "B" that is created will live on the heap. Class "A" is intended to have a vector of "B"'s as a private data member (ie. the class "A" can hold many "B"'s). I have a method in class "A" that will add another new instance of "B" in the vector that "A" has. I also have another method in class "A" that will update every single element that is in the vector of "B" instances. My questions are:</p> <p>How do I use the getters and setters within my struct?</p> <p>Here is some code that might help (note: grafix.h is a graphical library)</p> <pre><code>//.h file #pragma once #include &lt;vector&gt; #include "grafix.h" class A { public: ~A(); A(); void addB(); void updateB(); private: struct B { public: ~B() { delete p_b; p_b = 0; } B() { p_b = new B; //I need this here p_ball-&gt;x = 0; p_ball-&gt;y = 0; p_ball-&gt;r = 15; } void setxy(int X, int Y) { //What does this look like? //This? x = X; //? y = Y; } int retx() { //Likewise.... ? return x; //? } int rety() { return y; } void update() { draw(); //Movement code taken out for time being } private: int x,y; //xy int r; //radius B* p_b; //pointer to object, do I need this? void draw() { draw_circle_filled(x,y,r); //how can I access the data within the struct B here? //do I use the getters/setters? dereference?? } }; vector &lt;Ball*&gt; bs; //Holds all of the B's that have been created }; </code></pre> <p>And then the .cpp file that has the methods. This is where I have my question, am I typing in the right syntax??</p> <pre><code>#include "A.h" A::A() { addB(); } void A::addB() { B* b; bs.push_back(b); } void A::updateB() { for(int i = 0; i &lt; bs.size(); i++) { bs[i]-&gt;update(); } } </code></pre>
 

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