how to overload = operator for pointers

S

Suresh V

Hi,

I want to copy the contents of one pointer into another pointer which
is of same type by overloading '=' and '()' operator. Please guide me
how to do it. I tried doing it but only got syntax error and some
times crash.

class loc
{
public:
loc::loc() {
}

loc::loc(int longi, int lat) {
longitude = longi;
latitude =lat;
}
loc* operator()(loc op2);
void show();
private:
int latitude;
int longitude;
};

void loc::show()
{
cout << "long=" << longitude << " lat=" << latitude << endl;
}


loc* loc::eek:perator()(loc& op2)
{
cout << "pointer" << std::endl;
void* temp = malloc(sizeof(op2));
loc* loci = (loc *)temp;
loci->longitude = op2.longitude;
loci->latitude = op2.latitude;
return loci;
}

int main()
{
loc *new1 = new loc(4, 6);
loc *new2(newLoc);
new2->show();
delete new1;
delete new2;
return 0;
}


But i output
long=4 lat=6
"*** glibc detected *** double free or corruption (fasttop):
0x0000000000502010 ***"

+suresh
 
S

Suresh V

Hi,

I want to copy the contents of one pointer into another pointer which
is of same type by overloading '=' and '()' operator. Please guide me
how to do it. I tried doing it but only got syntax error and some
times crash.

class loc
{
public:
    loc::loc() {
    }

    loc::loc(int longi, int lat) {
        longitude = longi;
        latitude =lat;
    }
loc* operator()(loc op2);
void show();
private:
int latitude;
int longitude;

};

void loc::show()
{
cout << "long=" <<  longitude << " lat=" << latitude << endl;

}

loc* loc::eek:perator()(loc& op2)
{
cout << "pointer" << std::endl;
    void* temp = malloc(sizeof(op2));
    loc* loci = (loc *)temp;
    loci->longitude = op2.longitude;
    loci->latitude = op2.latitude;
    return loci;

}

int main()
{
loc *new1 = new loc(4, 6);
loc *new2(newLoc);
new2->show();
delete new1;
delete new2;
return 0;

}

But i output
long=4 lat=6
 "*** glibc detected *** double free or corruption (fasttop):
0x0000000000502010 ***"

+suresh

Correcting the syntax error:
int main()
{
loc *new1 = new loc(4, 6);
loc *new2(new1);
new2->show();
delete new1;
delete new2;
return 0;
}
 
V

Victor Bazarov

I want to copy the contents of one pointer into another pointer which
is of same type by overloading '=' and '()' operator. Please guide me
how to do it. I tried doing it but only got syntax error and some
times crash.

Overloading operators for built-in types (and pointers are built in) is
not allowed.

V
 
F

Francesco S. Carta

Correcting the syntax error:
int main()
{
loc *new1 = new loc(4, 6);
loc *new2(new1);

Apart from what Victor said, with the above statement you're simply
setting new2 to have the same content of new1 - that is, two pointers
storing the same exact address (print them out to see by yourself).

The obvious result is that you get the double deletion - even though the
code you posted isn't at all the code that caused that error, because
your code couldn't be compiled even after the self correction you made
in the follow-up.

Please read the C++ FAQ, you will learn a lot of things - among these,
you'll learn not to carelessly mix up C style memory management with C++
style.

FAQ 5.8 is particularly important: post complete, compilable code -
_the_ code that you compiled and that raised the errors you're
complaining about.
 
B

Bart van Ingen Schenau

Hi,

I want to copy the contents of one pointer into another pointer which
is of same type by overloading '=' and '()' operator. Please guide me
how to do it.

You can't overload the operators of an built-in type (like bald
pointers), so there is no way to do this.
Overloaded operators need to have at least one operand that is a user-
defined class. And for operator= and operator(), that even needs to be
the first (left hand side) operand.
I tried doing it but only got syntax error and some
times crash.
int main()
{
loc *new1 = new loc(4, 6);
loc *new2(newLoc);

Note that this does _not_ invoke an operator(). Instead it is a
declaration of an object that uses the constructor syntax to specify
the initial value of the pointer.
The effect is that new1 and new2 are both pointers to the same loc
object (the one created with new).

Bart v Ingen Schenau
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top