2 questions--virtual operators and operator=

Discussion in 'C++' started by c++novice, Apr 6, 2004.

  1. c++novice

    c++novice Guest

    1--Can operators be virtual?
    2--What is the difference between an operator= returning a refernce Vs a value?
     
    c++novice, Apr 6, 2004
    #1
    1. Advertising

  2. "c++novice" <> wrote in message
    news:...
    > 1--Can operators be virtual?


    Yes.

    > 2--What is the difference between an operator= returning a refernce Vs a

    value?

    Same as the difference between returning a reference or a value with any
    other function.

    john
     
    John Harrison, Apr 6, 2004
    #2
    1. Advertising

  3. "John Harrison" <> wrote in message
    news:c4v1sk$2o3lmb$-berlin.de...
    >
    > "c++novice" <> wrote in message
    > news:...
    > > 1--Can operators be virtual?

    >
    > Yes.
    >
    > > 2--What is the difference between an operator= returning a refernce Vs a

    > value?
    >
    > Same as the difference between returning a reference or a value with any
    > other function.
    >


    Maybe that wasn't the most helpful answer. Here's a short program, run it
    and see the result, then change the operator= return type to a value and run
    it again, see what difference it makes.

    #include <iostream>

    struct X
    {
    X& operator=(const X& rhs)
    {
    x = rhs.x;
    return *this;
    }
    int x;
    };

    int main()
    {
    X a, b, c;
    a.x = 1;
    b.x = 2;
    c.x = 3;
    (a = b) = c;
    std::cout << a.x << '\n';
    }

    But this needs quite an advanced understanding of C++. All you really need
    to know is returning a reference is the right thing to do.

    john
     
    John Harrison, Apr 6, 2004
    #3
  4. c++novice

    c++novice Guest

    "John Harrison" <> wrote in message news:<c4v1sk$2o3lmb$-berlin.de>...
    > "c++novice" <> wrote in message
    > news:...
    > > 1--Can operators be virtual?

    >
    > Yes.
    >
    > john


    perhaps an example would be more helpful.

    I am confused, I tried to write some code just to understand and see
    the behavior. can some body explain me.


    #include <iostream>

    using namespace std;

    class A {
    public:
    A() {
    this->Foo();
    }
    virtual void Foo() {
    cout << "A::Foo()" << endl;
    }
    virtual A& operator=(const A &rhs){
    cout << "A& operator=(const A&)" << endl;
    if(this == &rhs)
    return *this;
    return *this;
    }
    // virtual A &operator++();
    };

    class B : public A {
    public:
    B() {
    this->Foo();
    }
    virtual void Foo() {
    cout << "B::Foo()" << endl;
    }
    virtual B& operator=(const B &rhs){
    cout << "B& operator=(const B&)" << endl;
    if(this == &rhs)
    return *this;
    return *this;
    }
    // virtual B &operator++();
    };

    int main(int, char**)
    {
    A objA;
    B objB;
    objA = objB;

    B anotherobjB;
    anotherobjB = objA;
    return 0;
    }


    how can we call op= of class B?
    but op= if not declared, generated by compiler for every class then
    why virtual it.
     
    c++novice, Apr 7, 2004
    #4
  5. "c++novice" <> wrote in message
    news:...
    > "John Harrison" <> wrote in message

    news:<c4v1sk$2o3lmb$-berlin.de>...
    > > "c++novice" <> wrote in message
    > > news:...
    > > > 1--Can operators be virtual?

    > >
    > > Yes.
    > >
    > > john

    >
    > perhaps an example would be more helpful.
    >
    > I am confused, I tried to write some code just to understand and see
    > the behavior. can some body explain me.
    >
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > class A {
    > public:
    > A() {
    > this->Foo();
    > }
    > virtual void Foo() {
    > cout << "A::Foo()" << endl;
    > }
    > virtual A& operator=(const A &rhs){
    > cout << "A& operator=(const A&)" << endl;
    > if(this == &rhs)
    > return *this;
    > return *this;
    > }
    > // virtual A &operator++();
    > };
    >
    > class B : public A {
    > public:
    > B() {
    > this->Foo();
    > }
    > virtual void Foo() {
    > cout << "B::Foo()" << endl;
    > }
    > virtual B& operator=(const B &rhs){
    > cout << "B& operator=(const B&)" << endl;
    > if(this == &rhs)
    > return *this;
    > return *this;
    > }
    > // virtual B &operator++();
    > };
    >
    > int main(int, char**)
    > {
    > A objA;
    > B objB;
    > objA = objB;
    >
    > B anotherobjB;
    > anotherobjB = objA;
    > return 0;
    > }
    >
    >
    > how can we call op= of class B?


    You can't with that code. You have a A of the right hand side, but your
    operator= for B takes B on the right hand side. You cannot convert an A to a
    B. If you want operator= of B to be called when you have an A on the right
    hand side then you have to declare like this

    class B : public A
    {
    B& operator=(const B &rhs)
    {
    ...
    }
    B& operator=(const A &rhs)
    {
    ...
    }
    };

    virtual has got nothing to do with it.

    > but op= if not declared, generated by compiler for every class then
    > why virtual it.


    Why indeed? Why do you want a virtual operator=, what are you trying to
    achieve?

    You can certainly declare a virtual operator=, and it will behave exactly
    like any other virtual function. But it isn't much use.

    Here's some code that proves virtual operator= works.

    #include <iostream>
    using namespace std;

    class A
    {
    public:
    virtual A& operator=(int x) { cout << "A::eek:perator=\n"; return *this; }
    };

    class B : public A
    {
    public:
    virtual A& operator=(int x) { cout << "B::eek:perator=\n"; return *this; }
    };

    int main()
    {
    A* a_ptr = new B();
    *a_ptr = 1;
    }

    This code prints B::eek:perator= but if you remove the virtuals it prints
    A::eek:perator=, which shows that virtual operator= is just like any other
    virtual function.

    I think your problem is that you are expecting virtual to do something that
    it doesn't. Whatever it is that you are after I don't think virtual
    operator= is the way to go.

    If this hasn't helped then post again (perhaps in a new thread) and explain
    what it is that you are trying to achieve. Someone will show you the right
    way to do it.

    john
     
    John Harrison, Apr 8, 2004
    #5
  6. c++novice

    velthuijsen Guest

    Might want to take a look at http://www.parashift.com/c -faq-lite
    > #include <iostream>
    >
    > using namespace std;
    >
    > class A {
    > public:
    > A() {
    > this->Foo();


    This is not needed, the this-> is implictly added. And you might want
    to read Might want to take a look at
    http://www.parashift.com/c -faq-lite ,
    10.7 in the FAQ about the dangers of using this in the constructor (no
    danger in this case though).

    > }
    > virtual void Foo() {
    > cout << "A::Foo()" << endl;
    > }
    > virtual A& operator=(const A &rhs){
    > cout << "A& operator=(const A&)" << endl;
    > if(this == &rhs)
    > return *this;
    > return *this;



    Why bother with a selfassignment check if you don't do anything with
    the result?

    > }
    > // virtual A &operator++();
    > };
    >
    > class B : public A {
    > public:
    > B() {
    > this->Foo();
    > }
    > virtual void Foo() {


    > cout << "B::Foo()" << endl;
    > }
    > virtual B& operator=(const B &rhs){
    > cout << "B& operator=(const B&)" << endl;
    > if(this == &rhs)
    > return *this;
    > return *this;
    > }


    > // virtual B &operator++();
    > };
    >
    > int main(int, char**)


    please use int main () if you are not planning on using the command
    line args
    > {
    > A objA;
    > B objB;
    > objA = objB;


    if you ever want to do this it is better to write
    A* objA = new B;
    Do not forget to call delete when you don't need objA anymore or it
    will result in memory loss
    or write
    B objB;
    A* objA = &objB;
    The danger is here that you can call delete on objA releasing the
    memory that objB points to. Meaning that if you try to use objB from
    that point on your program should crash.

    Doing this will result in the virtual functions you have overridden to
    be accessed.
    if you write
    B objB;
    A* objA = &objB;
    objA->Foo();
    it will put to the screen B::Foo()

    Another point is that doing
    A objA;
    B objB;
    objA = objB;
    will result in slicing. Which means losing anything that the derived
    class added to the base class in the form of datamembers and
    functions.


    >
    > B anotherobjB;
    > anotherobjB = objA;


    This line should have generated an error. The compiler should complain
    that B doesn't have any operator= overloads that accept a right side
    of class A.
    Class B might be a class A but class A is not a class B. You need to
    explicitly specify a
    > return 0;
    > }
    >
    >
    > how can we call op= of class B?
    > but op= if not declared, generated by compiler for every class then
    > why virtual it.


    You need to explictly define
    virtual B& operator=(const A &rhs)
    {
    // do stuff
    }

    if that isn't the answer on your question please explain what you want
    more verbose please.
     
    velthuijsen, Apr 8, 2004
    #6
  7. c++novice

    c++novice Guest

    c++novice, Apr 8, 2004
    #7
    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. Michael Winter
    Replies:
    9
    Views:
    643
    Michael Winter
    Sep 24, 2003
  2. Alex Vinokur
    Replies:
    3
    Views:
    5,068
    Jeff Schwab
    Mar 20, 2005
  3. Calum
    Replies:
    7
    Views:
    358
    Calum
    Mar 14, 2006
  4. Replies:
    4
    Views:
    441
    Diego Martins
    Aug 15, 2006
  5. Jörg Rolef

    Virtual overloading of operators

    Jörg Rolef, Apr 26, 2010, in forum: C++
    Replies:
    4
    Views:
    823
Loading...

Share This Page