3

I've been using java but recently i had to switch to c++ and i am confused about some different things. My question is, in Java,

class Ex
{

   public static void main(String[]args){
       Ex e = func();
   }

   static Ex func(){
       Ex ee = new Ex();
       ee.x = 1; 
       ee.y = 2;
       return ee;
   }

}

class Ex
{
    int x;
    int y;
}

e and ee are same.

But in c++,

vector<int> func()
{
    vector<int>a;
    a.push_back(1);
    a.push_back(2);
    return a;
}

int main()
{
    vector<int>aa = func();
}

in this case, a and aa are the same?? if not same, every time I want to return vector, is it always copied to the calling area?

Parth Patel
  • 915
  • 11
  • 33
uma
  • 31
  • 1
  • In you c++ code, `a` and `aa` are not the same objects. And, yes, each time you call `func()` you perform a copy. – vahancho Jul 09 '19 at 08:27
  • `a` and `aa` and different objects. The C++ `func()` returns the data that makes up the object rather than a reference or pointer to the object. – khelwood Jul 09 '19 at 08:27
  • Moved currently (when copy-elision didn't be applied). – Jarod42 Jul 09 '19 at 08:27
  • Before the move mechanism, C++ will copy the data from your function into a new vector. In modern C++, you can move that memory or rely in some optimization provided by compilers. You want to probably check the RVO mechanism: https://www.ibm.com/developerworks/community/blogs/5894415f-be62-4bc0-81c5-3956e82276f3/entry/RVO_V_S_std_move?lang=en – mohabouje Jul 09 '19 at 08:27
  • 1
    As you've correctly observed the term "reference" has different meanings in both languages. Note that your C++ code doesn't use any reference at all (at least not how it is defined for C++ where the return value would have to be `vector&`). The Java term "reference" can be more thought of as a pointer in the C++ wording (not exactly but close enough). – Thomas Jul 09 '19 at 08:31

3 Answers3

1

The references in Java work more like pointers in C++ in some ways. The following C++ snippet would be closer to the Java code, although I wouldn't recommend writing the program that way:

vector<int>* func()
{
    vector<int> *a = new vector<int>;
    std::cout << "Address of a:  " << a << std::endl;
    a->push_back(1);
    a->push_back(2);
    return a;
}
int main()
{
    vector<int> *aa = func();
    std::cout << "Address of aa: " << aa << std::endl;
    delete aa;
}

Result:

Address of a:  01173338
Address of aa: 01173338

For a better idea of how references work in C++, consider the following code:

void addToVector(vector<int>& a) {
    a.push_back(2);
}

int main()
{
    vector<int> a;
    a.push_back(1);
    addToVector(a);
    std::cout << "Content of a: ";
    for (int& i : a)
        std::cout << i << " ";
    std::cout << endl;
}

Here, a ends up containing both 1 and 2 because we pass a reference of a to the function that modifies it. This is similar to how you can pass a reference to a function to modify the object in Java. If you remove the & in the signature of addToVector, however, then a will only end up containing 1 because what was passed to addToVector was just a copy that is discarded at the end of the function.

Instead of passing around the vector (which will be copied, at least some of it, as the actual content can be moved), the vector is now on the heap and only the pointer is passed. This is like in Java where a copy of the reference is returned rather than a copy of the actual object.

Blaze
  • 16,736
  • 2
  • 25
  • 44
  • Please don't recommend pointers+new to newbies. I suggest making that function so it modifies a given vector like `void func(std::vector&)`. That'd also help understand references because in java this wouldn't work. – nada Jul 09 '19 at 08:49
  • @nada good idea. I hope it's now clear that the code with the pointers was just for demonstration purposes and added an example that showcases reference in C++ (and how it differs from pass by copy). – Blaze Jul 09 '19 at 09:03
1

In C++, it used to be the case that a would be copied into aa every time func is called. But with modern C++ and Copy elision, the object is either directly constructor in the given memory (aa), or else a is constructed and then moved.

hasanou59
  • 63
  • 3
0

For starters this Java code

class Ex
{

   public static void main(String[]args){
      Ex e = func();
   }

    static Ex func(){
           Ex ee = new Ex();
           ee.x = 1; 
           ee.y = 2;
           return ee;
    }


}
class Ex
{
int x;
int y;
}

is incorrect. The class Ex is defined twice.

As for the C++ code

vector<int> func()
{
    vector<int>a;
    a.push_back(1);
    a.push_back(2);
    return a;

}
int main()
{
    vector<int>aa = func();
}

then after exiting the function func its local vector a will not alive. The vector in main can get the same extent of memory of its elements as the vector in the function had.

But the vector in main does not refer the variable a in the function though it "inherited" its data.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335