-
August 17th, 2011, 04:04 AM
#1
Operator Overloading
Hi there,
I'm trying to write a smart pointer class for an int variable but I've gotten a little stuck. Firstly, to declare a smart pointer to an int, I MUST use this line of code (As seen in the main method below):
smart_pointer s = new int();
To do this, I try to overload the "=" operator and set a pointer to an int (called "value" in the smart_pointer class) to the address of this new int. However, I get this error:
"conversion from ‘int*’ to non-scalar type ‘smart_pointer’ requested"
What am I doing wrong? I thought "new int()" returned a memory address which I could set the smart_pointer's int pointer to by overloading the "=" sign...
Thank you.
-------Code-------
#include<stdio.h>
#include<stdlib.h>
class smart_pointer{
private:
int *value;
public:
smart_pointer();
// Overwrite "=" operator.
smart_pointer &operator=(int *val){
value = val;
return *this;
}
~smart_pointer(){
delete value;
}
};
smart_pointer::smart_pointer(){
value = NULL;
}
int main(void){
smart_pointer s = new int();
return 0;
}
-
August 17th, 2011, 04:54 AM
#2
Re: Operator Overloading
 Originally Posted by ace_of_pentacles
To do this, I try to overload the "=" operator
You are mistaken. This:
Code:
smart_pointer s = new int();
is equivalent to:
Code:
smart_pointer s(new int());
i.e., you should write a constructor instead (or perhaps, in addition to overloading operator= since you should also define or disable the copy constructor and copy assignment operator)
-
August 17th, 2011, 05:12 AM
#3
Re: Operator Overloading
Looking at your implementation, you might be interested in just using auto_ptr instead.
If you have access to boost or C++0x, then you could look into unique_ptr and shared_ptr.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
August 17th, 2011, 05:17 AM
#4
Re: Operator Overloading
Ah, I see. Of course, the constructor would have to be the first thing to be called.
I feel a little silly now.
Thank you for taking the time to help me out.
-
August 17th, 2011, 05:25 AM
#5
Re: Operator Overloading
Last edited by monarch_dodra; August 17th, 2011 at 05:31 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
August 17th, 2011, 05:31 AM
#6
Re: Operator Overloading
Remember to make the constructor explicit. Inadvertently placing a pointer into an object that claims ownership of the pointer's life is one of the last things you want to do.
[EDIT]Whilst were on the subject, having "smart_pointer &operator=(int *val)" and being able to write "smart_pointer s = new int();" are both very bad ideas, for the same reason.
You'll want a named member function to do that.
PS: There is a flaw in your operator= implementation: What if your smart_pointer already had a value?
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|