Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to learn about references and pointers in C++.</p> <p>These are accesses to existing objects (think "a link to"). The main difference between the two is that there is that if you give a reference to an object, you guarantee that that object exists at the time you pass. If you give a pointer, there may be nothing behind it, this is referred as a "null" pointer (written <code>NULL</code> or <code>nullptr</code>) in recent versions.</p> <p>Here is the main way to use both:</p> <pre><code>int i = 0; int&amp; refToI = i; // create a reference to i int* ptrToI = &amp;i; // create a pointer to i int&amp; anotherRef; // invalid, references must be initialized; int* anotherPtr; // valid, points to garbage data, better set it to NULL though refToI = 1; // i has been changed *ptrToI = 1; // i has been changed, the "*" operator means "content pointed by" </code></pre> <p>Objects (or functions) accepting a reference or a pointer must clarify the lifetime they expect from the pointed objects. That is, once a reference or pointer is created, the object it points too may be destroyed. That's bad, avoid it.</p> <p>Here are some examples in your case:</p> <pre><code>// Solution 1 take a reference to the objet in the function using it class Job; // tells the compiler that there's a Job class somewhere. class Display { public: void DoWork(Job&amp; job) { // write this in the CPP file, I am using a shortcut for the sake of brevity job.DoSomething(); } }; // usage: Display display; Job job; display.DoWork(job); // Solution 2, carry the reference (or pointer) inside the class using it class Job; // tells the compiler that there's a Job class somewhere. class Display { public: Display(Job&amp; job) // !! job must live at least as long as display : _job(job) { } void DoWork() { // write this in the CPP file, I am using a shortcut for the sake of brevity _job.DoSomething(); } private: Job&amp; _job; }; // Usage: Job job; Display display(job); // job and display have the same lifetime, OK display.DoSomething(); </code></pre> <h2>NOTE</h2> <p>There's a debate on whether you should use pointers or references. I think this is a bit harder for you right now. A rule of thumb is use references when lifetimes are well known, use <em>smart</em> pointers when they're not or when using <code>new</code>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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