CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2011
    Posts
    4

    Question 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;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Operator Overloading

    Quote 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)
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  4. #4
    Join Date
    Aug 2011
    Posts
    4

    Smile 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.

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Operator Overloading

    [EDIT] Double post
    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.

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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
  •  





Click Here to Expand Forum to Full Width

Featured