operator= - code in overloaded function doesn't get called



 DEVELOP > c-Plus-Plus > operator= - code in overloaded function doesn't get called

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Tobias Langner"
Date: 31 Jul 2003 03:49:11 AM
Object: operator= - code in overloaded function doesn't get called
I overloaded the operator= - but in the program, if the line p3=p1 gets
executed, the program doesn't jump to the method.
The class definition and the test code.
template <class T, template <class> class OwnershipPolicy>
class SmartPtr : public OwnershipPolicy<T> {
public:
explicit SmartPtr(T* pointer);
SmartPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
SmartPtr<T, OwnershipPolicy> &operator=(const SmartPtr<T, OwnershipPolicy>
&);
~SmartPtr();
T& operator*() const;
T* operator->() const;
private:
void deleteObject();
void copyPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
SmartPtr();
T* pointer_;
};
//The Testcode:
bool testSmartPtr() {
SmartPtr<LongWrapper, RefCount> *p1=new SmartPtr<LongWrapper,
RefCount>(new LongWrapper(1));
SmartPtr<LongWrapper, RefCount> *p2=new SmartPtr<LongWrapper,
RefCount>(new LongWrapper(20));
SmartPtr<LongWrapper, RefCount> *p3=new SmartPtr<LongWrapper,
RefCount>(*p2);


std::cout<<"p1=p3\n";
p3=p1;
std::cout<<"delete p1\n";
delete p1;
std::cout<<"delete p2\n";
delete p2;
std::cout<<"delete p3\n";
delete p3;

return true;
}
.

User: "John Harrison"

Title: Re: operator= - code in overloaded function doesn't get called 31 Jul 2003 04:22:19 AM
"Tobias Langner" <tobias.langner@t-online.de> wrote in message
news:bgal7l$16c$07$2@news.t-online.com...

I overloaded the operator= - but in the program, if the line p3=p1 gets
executed, the program doesn't jump to the method.

The class definition and the test code.

template <class T, template <class> class OwnershipPolicy>
class SmartPtr : public OwnershipPolicy<T> {
public:
explicit SmartPtr(T* pointer);
SmartPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
SmartPtr<T, OwnershipPolicy> &operator=(const SmartPtr<T,

OwnershipPolicy>

&);
~SmartPtr();
T& operator*() const;
T* operator->() const;

private:
void deleteObject();
void copyPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
SmartPtr();
T* pointer_;

};

//The Testcode:

bool testSmartPtr() {
SmartPtr<LongWrapper, RefCount> *p1=new SmartPtr<LongWrapper,
RefCount>(new LongWrapper(1));
SmartPtr<LongWrapper, RefCount> *p2=new SmartPtr<LongWrapper,
RefCount>(new LongWrapper(20));
SmartPtr<LongWrapper, RefCount> *p3=new SmartPtr<LongWrapper,
RefCount>(*p2);



std::cout<<"p1=p3\n";
p3=p1;

std::cout<<"delete p1\n";
delete p1;
std::cout<<"delete p2\n";
delete p2;
std::cout<<"delete p3\n";
delete p3;

return true;
}

Well that's because p1, p2 and p3 are pointers.
I think perhaps what you meant to write is
SmartPtr<LongWrapper, RefCount> p1=SmartPtr<LongWrapper, RefCount>(new
LongWrapper(1));
SmartPtr<LongWrapper, RefCount> p2=SmartPtr<LongWrapper, RefCount>(new
LongWrapper(20));
SmartPtr<LongWrapper, RefCount> p3=p2;
std::cout<<"p1=p3\n";
p3=p1;
You seem to be getting your smart pointers and your ordinary pointers mixed
up.
john
.

User: "Jakob Bieling"

Title: Re: operator= - code in overloaded function doesn't get called 31 Jul 2003 04:22:26 AM
"Tobias Langner" <tobias.langner@t-online.de> wrote in message
news:bgal7l$16c$07$2@news.t-online.com...

I overloaded the operator= - but in the program, if the line p3=p1 gets
executed, the program doesn't jump to the method.

The class definition and the test code.

template <class T, template <class> class OwnershipPolicy>
class SmartPtr : public OwnershipPolicy<T> {
public:
explicit SmartPtr(T* pointer);
SmartPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
SmartPtr<T, OwnershipPolicy> &operator=(const SmartPtr<T,

OwnershipPolicy>

&);
~SmartPtr();
T& operator*() const;
T* operator->() const;

private:
void deleteObject();
void copyPtr(const SmartPtr<T, OwnershipPolicy>& otherPointer);
SmartPtr();
T* pointer_;

};

//The Testcode:

bool testSmartPtr() {
SmartPtr<LongWrapper, RefCount> *p1=new SmartPtr<LongWrapper,
RefCount>(new LongWrapper(1));
SmartPtr<LongWrapper, RefCount> *p2=new SmartPtr<LongWrapper,
RefCount>(new LongWrapper(20));
SmartPtr<LongWrapper, RefCount> *p3=new SmartPtr<LongWrapper,
RefCount>(*p2);



std::cout<<"p1=p3\n";
p3=p1;

'p3' and 'p1' are pointers, not objects. That is why the operator= for
the 'object' does not get called, but the operator= for the 'pointer'. The
original value for 'p3' is lost, meaning you also have a memory leak.

std::cout<<"delete p1\n";
delete p1;
std::cout<<"delete p2\n";
delete p2;
std::cout<<"delete p3\n";
delete p3;

You have undefined behaviour here, because above you made 'p1' and 'p3'
point to the same object, which you now try to delete a second time. Your
program might have crashed here.

return true;
}

hth
--
jb
(replace y with x if you want to reply by e-mail)
.


  Page 1 of 1

1

 


Related Articles
How can I get the address of a virtual member function?
How to get name of calling function
How can I get the address of a virtual member function?
get() function vs public member
Get address of function, not entry in jump table.
I want to get member function pointer...
Is there any function in STL to get the equivalence of two sets?
How can I get the address of a virtual member function?
how can I get the call __PRETTY_FUNCTION__ runing?
Does the vtable poiinter get reset with an ifstream read function????
One type traits class (or something like that) to get the return type of a pointer to function
Can I use the map-constructor in a clever way and get rid of this function?
How to get and pass data to a class member function
Re: For god's sake how do I get rid of this warning!
Get all files list in a directory
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER