overload of operator=

Discussion in 'C++' started by paolo.dx@gmail.com, Apr 6, 2006.

  1. Guest

    Hi, I have to overload operator= to call a method of a class, this is
    what I need.

    template<class I>
    class port{
    I dato;

    public:
    void write(I new_dato){
    dato = new_dato;
    };

    I read(){
    return(dato);
    };

    void operator= (I dato){
    this->write(dato);
    };
    };

    int main(){

    int k,j;
    port<int> my_port;

    k=5;
    my_port = k;
    j = my_port; //i need to overload operator= to do my_port.read();

    };

    obviously I can call my_port.read() and my_port.write(k) but also I
    have to be able to use the operator =
    The overload for .write() is not a problem because the left side object
    is an instance of class port, but how can I do for the .read()?
    The operator= have to call the method .read() of the right side object,
    and the left side is a standard type of c++.

    Thanks in advance.
     
    , Apr 6, 2006
    #1
    1. Advertising

  2. wrote:
    > Hi, I have to overload operator= to call a method of a class, this is
    > what I need.
    >
    > template<class I>
    > class port{
    > I dato;
    >
    > public:
    > void write(I new_dato){
    > dato = new_dato;
    > };
    >
    > I read(){
    > return(dato);
    > };
    >
    > void operator= (I dato){
    > this->write(dato);
    > };
    > };
    >
    > int main(){
    >
    > int k,j;
    > port<int> my_port;
    >
    > k=5;
    > my_port = k;
    > j = my_port; //i need to overload operator= to do my_port.read();
    >
    > };
    >
    > obviously I can call my_port.read() and my_port.write(k) but also I
    > have to be able to use the operator =
    > The overload for .write() is not a problem because the left side
    > object is an instance of class port, but how can I do for the .read()?
    > The operator= have to call the method .read() of the right side
    > object, and the left side is a standard type of c++.


    You cannot overload assignment to any built-in types. Your best solution
    would be to provide the conversion function in your class template, which
    will return a value of the type 'I':

    template<class I> class port {
    ...
    operator I() const { return dato; }
    };

    The compiler will pick this conversion where you give 'port<X>' and it
    expects an 'X'.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 6, 2006
    #2
    1. Advertising

  3. Guest

    Thank you Victor, your solution work very well for my problem.

    Paolo
     
    , Apr 7, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr Smith
    Replies:
    7
    Views:
    478
    Mr Smith
    Feb 10, 2005
  2. Matthew Monopole

    overload operator -

    Matthew Monopole, Aug 8, 2003, in forum: C++
    Replies:
    1
    Views:
    483
    Jakob Bieling
    Aug 8, 2003
  3. Jakob Bieling

    Q: Overload operator new/delete

    Jakob Bieling, Aug 8, 2003, in forum: C++
    Replies:
    0
    Views:
    406
    Jakob Bieling
    Aug 8, 2003
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    377
    Nick Hounsome
    Jan 25, 2004
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    263
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page