Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a reference constructor?

I am looking for a way to have my constructor make a reference to another object in this way:

Foo object1("File1");
Foo object2("File1");

The first object is created normally. The second object sees that there is already an object that is using the "File1" parameter and makes itself a reference to the first object. I know this may not be directly possible to do this in this manner. I keep a static vector of Foo* to keep track of the allocated objects.

I know that I can make all the members of the class pointers and for the first case create (New) them. In the second case, I would not create them and point them at the first object. Also the second object is guaranteed to have a shorter lifetime than the first.

So, is there an easy/elegant way to do this?

EDIT: Thank you for all the great solutions. I like them all, but I can only use one. I will keep all of this knowledge for future reference tho.

I am selecting the static map <string, FooObject*> solution. At first I thought it was stupid, but upon further review, it appealed to me as elegant.

The only addition that I can see right now would be to add a link counter to the FooObject. This way in the constructor of Foo I can increment the link counter. In the destructor, decrement the counter. If counter is then zero, remove it from the map. This way there is no memory leaks, and the objects can get destructed in any order. I guess this methodology is shared_ptr esque. Credit goes to @Travis for this.

Thanks, James.

like image 440
SuperJames Avatar asked Mar 10 '11 16:03

SuperJames


People also ask

How do you reference a constructor?

A constructor reference can be created using the class name and a new keyword. The constructor reference can be assigned to any functional interface reference that defines a method compatible with the constructor.

How do you initialize a reference in a constructor?

There are three steps to initializing a reference variable from scratch: declaring the reference variable; using the new operator to build an object and create a reference to the object; and. storing the reference in the variable.

How do you implement a constructor in Java?

The two rules for creating a constructor are: The name of the constructor should be the same as the class. A Java constructor must not have a return type. Default Constructor - a constructor that is automatically created by the Java compiler if it is not explicitly defined.


2 Answers

Try to use static map<string, object*> as variable where object is the rest of the class you need.

like image 98
The GiG Avatar answered Oct 02 '22 23:10

The GiG


You can use a resource manager. It should have an interface like this:

class ResourceManager
{
public:
   Foo* GetFoo(some id type, maybe file name);
   void ReleaseFoo(Foo* fooObj);
}

Inside it will search if there is Foo* object that uses same id like the new id and if this exits it will return old Foo* object else it will create a new object of type Foo*. It will also count the number of Foo* objects for every id. When Release function is called it will decrement the number for that id(that identifies in a unique the object) and if it's 0 it will delete the object.

This is the most simple way of doing this. You can have it running in another thread for more that one type of resource and other stuff, but this is the basic idea.

like image 37
Mircea Ispas Avatar answered Oct 03 '22 00:10

Mircea Ispas