Pass by reference to a template function through parameter only

Due to the nature of this requirement, I've made a very minimal example, which would adequately solve my issue, without resorting to use of pointers or copy constructors.

Basically I'm trying to pass an object as a reference to the template function, rather than a copy as it's seeing. I'm needing to do this without editing Obj::Call to accommodate a reference as its first parameter, as it'd break other calls.

You'll notice in the following code the object will be destroyed upon passing, while the object defined is still in-scope due to the infinite end loop.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;

class Obj
{
public:
   string name;

   Obj(string name): name(name) {cout << "create " << this << endl;}
   ~Obj() {cout << "destroy" << endl;}

   template <class T>
      static void Call(T value)
   {
      cout << "Value: " << &value << endl;
   }
};

int main()
{
   Obj obj("Hello");
   Obj::Call(obj);
   while(1);
}


In the past I tried ref(), which appeared to stop this happening, however it created a blank copy of the object instead.
Last edited on
Please do not crosspost. It clutters forum, spreads discussion on your problem between two threads and generally annoy people.

Another post: http://www.cplusplus.com/forum/general/142801/
Topic archived. No new replies allowed.