What is the best way to use Java-style references in C++? Will using shared_ptr
for all classes and structs be the correct equivalent?
Asked
Active
Viewed 99 times
0

Utka
- 49
- 6
-
2Java and C++ are different languages. They are not equivalent and take different coding styles. Trying to code in C++ as if it was Java is not an appropriate way to write C++ – Avi Berger Nov 21 '16 at 22:25
-
shared_ptr (and its siblings) is the correct way to get (almost) effortless memory management in c++ – pm100 Nov 21 '16 at 22:25
-
1No. Java has extra defenses against cycles that `shared_ptr` lacks. in C++ the recommendation is to not use a dynamic allocation unless absolutely necessary. – user4581301 Nov 21 '16 at 22:25
-
1@user4581301 any non trivial C++ app has dynamic memory allocation – pm100 Nov 21 '16 at 22:28
-
2There is no mechanical translation from Java to C++. Some uses of Java references can be appropriately implemented by shared pointers, but I'd say that's a very small minority, since shared pointers only have a very niche use in C++. There are many other ways of passing arguments in C++ that will be better in many situations. – Kerrek SB Nov 21 '16 at 22:28
-
Sorry, can't upvote the comments. Why is the answer abour RAII downvoted, is it wrong? – Utka Nov 21 '16 at 22:30
-
its not really relevant to your question – pm100 Nov 21 '16 at 22:30
-
@KerrekSB could you expand on it a bit, why do they only have a niche use only? – Utka Nov 21 '16 at 22:32
-
@pm100 RAII is idiom for exception safe resource management. Why it is not relevant? It is even better since it also manages other resources besides memory like sockets, locks or file descriptors. – Öö Tiib Nov 21 '16 at 22:32
-
@ÖöTiib Probably because the question isn't about the C++ idioms for exception safe resource management. – juanchopanza Nov 21 '16 at 22:33
-
3@Utka: Because it's very rare that an object truly has *shared* ownership. In the overwhelming majority of cases, an object has a clear, unique owner. – Kerrek SB Nov 21 '16 at 22:33
-
What is the question about if it is not about resource management in C++? – Öö Tiib Nov 21 '16 at 22:37
-
one of the problems I often face is that I realize later on, that I need polymorphic objects instead of my original ones. Which means I need pointers instead of objects and raw pointers seemed (to me) to become unpopular in modern C++. – Utka Nov 21 '16 at 22:39
-
1@Utka There are different ways of solving that problem, but imitating java isn't a good one. It doesn't play to C++'s strengths. – juanchopanza Nov 21 '16 at 22:43
-
@pm100 Agree, and it looks like OP has found a good case for dynamic allocation. Utka: Give this a read: [Which kind of pointer do I use when?](http://stackoverflow.com/questions/8706192/which-kind-of-pointer-do-i-use-when) – user4581301 Nov 21 '16 at 22:44
-
@ÖöTiib I think resource management is relevant, as long as it can solve the problem. To me, Java references have two main properties - they will be automatically freed and they are polymorphic. They are also "easy" to use, whereas I would say, that writing a wrapper for every class (possibly even for standard ones) is not an easy solution. – Utka Nov 21 '16 at 22:45
1 Answers
0
I highly recommend reading this answer for understanding C++ smart pointers.
Now, if by 'Java-style references' you mean you want to pass an object or struct into a method and that passed in object is not a copy of the object, but rather a reference to the object, then simply use pass by reference.
Ex:
void foo( int& ref, int val)
{
ref = ref + 1; // pass by reference
val = val + 1; // pass by value
}
...
int main()
{
int a = 10;
int b = 10;
foo(a, b);
// a is being passed by reference so foo directly modifies a
std::cout << "a = " << a << " b = " << b;
// output would be: a = 11 b = 10
}