overload "="

Discussion in 'C++' started by 0infinity0@gmail.com, Jul 14, 2006.

  1. Guest

    Why wont the operator work

    #include <iostream.h>
    class A
    {
    public:
    A * operator = (A * rhs)
    {
    cout << "Ptr cpy" << endl;
    return this;
    }

    private:
    };

    main()
    {
    A *a1;

    A* a2 = new A();

    a1 = a2;
    }
     
    , Jul 14, 2006
    #1
    1. Advertising

  2. * :
    > Why wont the operator work
    >
    > #include <iostream.h>


    That's a non-standard header.


    > class A
    > {
    > public:
    > A * operator = (A * rhs)
    > {
    > cout << "Ptr cpy" << endl;
    > return this;
    > }
    >
    > private:
    > };
    >
    > main()


    'main' must have result type 'int'.


    > {
    > A *a1;
    >
    > A* a2 = new A();
    >
    > a1 = a2;


    You're assigning to the pointer a1. Your operator only comes into play
    if you assign to an object of type A. The pointer a1 is of type A*.


    > }
    >



    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jul 14, 2006
    #2
    1. Advertising

  3. Guest

    Thx...

    Alf P. Steinbach wrote:
    > * :
    > > Why wont the operator work
    > >
    > > #include <iostream.h>

    >
    > That's a non-standard header.
    >
    >
    > > class A
    > > {
    > > public:
    > > A * operator = (A * rhs)
    > > {
    > > cout << "Ptr cpy" << endl;
    > > return this;
    > > }
    > >
    > > private:
    > > };
    > >
    > > main()

    >
    > 'main' must have result type 'int'.
    >
    >
    > > {
    > > A *a1;
    > >
    > > A* a2 = new A();
    > >
    > > a1 = a2;

    >
    > You're assigning to the pointer a1. Your operator only comes into play
    > if you assign to an object of type A. The pointer a1 is of type A*.
    >
    >
    > > }
    > >

    >
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
     
    , Jul 14, 2006
    #3
  4. Default User Guest

    wrote:

    > Thx...


    > > A: Because it messes up the order in which people normally read
    > > text. Q: Why is it such a bad thing?
    > > A: Top-posting.
    > > Q: What is the most annoying thing on usenet and in e-mail?



    Argh.




    Brian
     
    Default User, Jul 14, 2006
    #4
    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. Eph0nk

    Error: Overload resolution failed

    Eph0nk, Oct 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,862
    Eph0nk
    Oct 23, 2003
  2. MattB

    Overload

    MattB, Dec 22, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    546
    MattB
    Dec 23, 2003
  3. Babs
    Replies:
    0
    Views:
    3,066
  4. Piotre Ugrumov
    Replies:
    3
    Views:
    375
    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:
    255
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page