Does this class will make a pointer in c++ work like the reference in java

Z

Zheng Da

Will the following class work well?
If it can not work correctly in some situation, could you help me to
fix it?
Thank you.

//the class will work like the reference in java
//when you create a object, you must save the pointer of the object in
super_auto_ptr
//the class cannot work correctly in multithread

templete<T>
class super_auto_ptr
{
class in_ptr
{
int ref;
T *ptr;
public:
in_ptr(T *p)
{
ptr=p;
ref=1;
}

~in_ptr()
{
delete ptr;
}
};
in_ptr *ptr;

struct super_auto_ptr_ref
{
in_ptr *ptr;
super_auto_ptr_ref(in_ptr *p)
{
ptr=p;
}
};

public:
//the pointer will be saved in in_ptr
super_auto_ptr(T *p)
{
ptr=new in_ptr(p);
}

super_auto_ptr(super_auto_ptr &ref)
{
ptr=ref.ptr;
ptr->ref++;
}

~super_auto_ptr()
{
ptr->ref--;
if(ptr->ref == 0)
delete ptr;
}

super_auto_ptr& operator=(super_auto_ptr &rhs)
{
if(rhs.ptr != this->ptr)
{
this->ptr->ref--;
if(ptr->ref == 0)
delete ptr;
rhs.ptr->ref++;
this->ptr=rhs.ptr;
}
}

T *operator->()
{
return ptr->ptr;
}

T &operator*()
{
return *ptr;
}

super_auto_ptr(super_auto_ptr_ref rhs)
{
this->ptr=rhs.ptr;
this->ptr->ref++;
}

super_auto_ptr &operator=(super_auto_ptr_ref rhs)
{
if(rhs.ptr != this->ptr)
{
this->ptr->ref--;
if(ptr->ref == 0)
delete ptr;
rhs.ptr->ref++;
this->ptr=rhs.ptr;
}
}

operator super_auto_ptr_ref()
{
return super_auto_ptr_ref(ptr);
}
};
 
K

Kristo

Zheng said:
Will the following class work well?
If it can not work correctly in some situation, could you help me to
fix it?
Thank you.

//the class will work like the reference in java
//when you create a object, you must save the pointer of the object in
super_auto_ptr
//the class cannot work correctly in multithread

I think this is a waste of your time. Java references are guaranteed
to either point to a valid object or null. There can be no wild
references. In C++ you have no such guarantee. You have no way of
knowing that an arbitrary pointer points to a valid object. In short,
if you want Java, go write Java. Now, if you want to just write a good
smart pointer, there are plenty of resources out there to help you with
that. The Boost libraries, for example, have several smart pointers
that you could use as a starting point.
templete<T>

Next time please post compilable code. We recommend copy and pasting
from your text editor instead of typing it directly.
class super_auto_ptr
{
class in_ptr
{
int ref;
T *ptr;
public:
in_ptr(T *p)
{
ptr=p;
ref=1;
}

Use the initializer list for this:

in_ptr(T *p) : ref(1), ptr(p)
{
}
~in_ptr()
{
delete ptr;
}
};
in_ptr *ptr;

struct super_auto_ptr_ref
{
in_ptr *ptr;
super_auto_ptr_ref(in_ptr *p)
{
ptr=p;
}

Use the initializer list here...
};

public:
//the pointer will be saved in in_ptr
super_auto_ptr(T *p)
{
ptr=new in_ptr(p);
}

....and here.
super_auto_ptr(super_auto_ptr &ref)
{
ptr=ref.ptr;
ptr->ref++;
}

You can use it for copy constructors too:

super_auto_ptr(super_auto_ptr &ref) : ptr(ref.ptr)
{
++ptr->ref;
}
~super_auto_ptr()
{
ptr->ref--;
if(ptr->ref == 0)
delete ptr;
}

super_auto_ptr& operator=(super_auto_ptr &rhs)
{
if(rhs.ptr != this->ptr)
{
this->ptr->ref--;
if(ptr->ref == 0)
delete ptr;
rhs.ptr->ref++;
this->ptr=rhs.ptr;
}
}

T *operator->()
{
return ptr->ptr;
}

T &operator*()
{
return *ptr;
}

super_auto_ptr(super_auto_ptr_ref rhs)
{
this->ptr=rhs.ptr;
this->ptr->ref++;
}

Initializer list again.
super_auto_ptr &operator=(super_auto_ptr_ref rhs)
{
if(rhs.ptr != this->ptr)
{
this->ptr->ref--;
if(ptr->ref == 0)
delete ptr;
rhs.ptr->ref++;
this->ptr=rhs.ptr;
}
}

operator super_auto_ptr_ref()
{
return super_auto_ptr_ref(ptr);
}

operator is a reserved keyword, so it can't be a type. ITYM
'super_auto_ptr_ref operator()'.

Kristo
 
M

Marcin Kalicinski

operator super_auto_ptr_ref()
operator is a reserved keyword, so it can't be a type. ITYM
'super_auto_ptr_ref operator()'.

No, I think OP got it right here. This is operator of conversion to
super_auto_ptr_ref.

cheers,
M.
 
Z

Zheng Da

I'm sorry. I copied from a wrong file, so there is something wrong in
the code.
And I wrote a c++ program, and find it's very hard to avoid the memory
leak.
I have tried to use auto_ptr, but it cannot work as I want
By the way, I wrote the class just for avoiding the memory leak,
and I do not want it work the same as the reference in Java
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top