Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to return a reference in C++
    primarykey
    data
    text
    <p>If for example I create a new object in a function and I want to return its reference how can I do it?</p> <p>Lets say I got an IntClass that has a 'int num' field in the private. So i want to create a new element of IntClass in the function and return the reference of that object. I've tried something like that but it seems illegal (from some reason when I do such thing the Binaries gets deleted when I compile the code although I get no errors from the compiler itself (ECLIPSE):</p> <pre><code> IntClass* a = new IntClass(10); IntClass &amp;ref = *a; return ref; </code></pre> <p>Any ideas how to return a reference then?</p> <p>EDIT: It's allowed to "assume" that C++ uses a Garbage-Collection</p> <p>As it's not clear to most of you WHY I "want" to do it so here is the full question:</p> <p>I got this file (Not made by me and can't be edited):</p> <p><strong>poly_subtype.h</strong>:</p> <pre><code>#ifndef POLY_SUBTYPE_H #define POLY_SUBTYPE_H #include &lt;iostream&gt; #include "cpu_add.h" using std::cout; using std::endl; //Deriving classes definition class IntClass; class DoubleClass; //The Virtual Number Class. IntClass and FloatClass will derive from this class. class Number { public: //return a Number object that's the results of x+this, when x is DoubleClass virtual Number&amp; addDouble(DoubleClass&amp; x) = 0; //return a Number object that's the results of x+this, when x is IntClass virtual Number&amp; addInt(IntClass&amp; x) = 0; //return a Number object that's the results of x+this, when x is either //IntClass or DoubleClass virtual Number&amp; operator+(Number&amp; x) = 0; //Print the number stored in the object virtual void print_number() = 0; }; class IntClass : public Number { private: int my_number; public: //Constructor IntClass(int n):my_number(n) {} //returns the number stored in the object int get_number() {return my_number;} //print the number stored in the object void print_number() {cout &lt;&lt; my_number &lt;&lt; endl;} //return a DoubleClass object that's the result of x+this Number&amp; addDouble(DoubleClass&amp; x); //return an IntClass object that's the result of x+this Number&amp; addInt(IntClass&amp; x); //return a Number object that's the result of x+this. //The actual class of the returned object depends on x. //If x is IntClass, then the result if IntClass. //If x is DoubleClass, then the results is DoubleClass. Number&amp; operator+(Number&amp; x); }; class DoubleClass : public Number { private: double my_number; public: //Constructor DoubleClass(double n):my_number(n) {} //returns the number stored in the object double get_number() {return my_number;} //Print the number stored in the object void print_number() {cout &lt;&lt; my_number &lt;&lt; endl;} //return a DoubleClass object that's the result of x+this Number&amp; addDouble(DoubleClass&amp; x); //return a DoubleClass object that's the result of x+this Number&amp; addInt(IntClass&amp; x); //return a DoubleClass object that's the result of x+this. //This should work if x is either IntClass or DoubleClass Number&amp; operator+( Number&amp; x); }; #endif </code></pre> <p>and I've got this file (again, not written by me and can't be edited - says I can use those functions but not the '+' operator.</p> <p><strong>cpu_add.h:</strong></p> <pre><code>#ifndef CPU_ADD_H #define CPU_ADD_H double add_double_double(double a, double b) {return (a+b);} double add_int_double(int a, double b) {return ((double)(a)+b);} int add_int_int(int a, int b) {return (a+b);} #endif </code></pre> <p>My goal is to implement the next functions (which you can find their declarations and functionality in <strong>poly_subtype.h</strong> above):</p> <pre><code>Number&amp; IntClass::addInt(IntClass&amp; x); Number&amp; IntClass::addDouble(DoubleClass&amp; x); Number&amp; IntClass::operator+(Number&amp; x); Number&amp; DoubleClass::addInt(IntClass&amp; x); Number&amp; DoubleClass::addDouble(DoubleClass&amp; x); Number&amp; DoubleClass::operator+(Number&amp; x); </code></pre> <p>I hope it's more clear now.</p> <p>For example, <strong>AddInt</strong>:</p> <pre><code>Number&amp; IntClass::addInt(IntClass&amp; x){ int num = add_int_int(my_number, x.get_number()); IntClass* a = new IntClass(num); IntClass &amp;ref = *a; return ref; } </code></pre>
    singulars
    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.
 

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