Thread: virtual operator()

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248

    virtual operator()

    Hi there,

    I want to pass a Functor interface as a function argument :

    Code:
    class IFoo
    {
     public:
      virtual ~IFoo(){}
      virtual bool operator()() const = 0;
    }
    class Foo : public IFoo
    {
     public:
      bool operator()() const;
    }
    Now a small voice tells me that the operator() cannot be virtual, and therefore a function "apply" should be implemented. But when i try it compiles and runs.

    Does anyone know issues about this, or should i just ignore that small voice..

    Thanks!

    Mark

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Why shouldn't it be allowed to be virtual?

    Operators are like regular functions in the vtable - they just have "strange" names.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Yes yes I know, but I am so convinced I've read something about it..
    I know the operator= cannot be inherited , but yeah for the others I didn't see any reason why neither...

    Probably a very bad source or a bad interpretation from my side...

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I know the operator= cannot be inherited
    Yes it can.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well it can be, but it just doesn't seem to be B's operator=

    Code:
    #include <iostream>
    
    class A
    {
    public:
        virtual const A& operator = (const A&)
        {
            std::cout << "A=A\n";
            return *this;
        }
    };
    
    class B: public A
    {
    public:
        const A& operator= (const A&)
        {
            std::cout << "B=?\n";
            return *this;
        }
    };
    
    int main()
    {
        B b, c;
        A* pb = &b;
        A* pc = &c;
        *pb = *pc;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    What's wrong with your sample operator= code? I just ran it, and it runs as expected. The last line in main is what triggers the overloaded = operator. Since the object at *pb is of type B, the vtable points to the correct function.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Do you think that '?' is there by accident?

    Perhaps this update will explain the problem a little better.

    Soma

    Code:
    #include <iostream>
    
    class A
    {
    public:
        virtual const A& operator = (const A&)
        {
            std::cout << "A=A\n";
            return *this;
        }
    };
    
    class B: public A
    {
    public:
        const A& operator= (const A&)
        {
            std::cout << "B=?\n";
            return *this;
        }
    };
    
    class C: public A
    {
    public:
        const A& operator= (const A&)
        {
            std::cout << "C=?\n";
            return *this;
        }
    };
    
    int main()
    {
        B b;
        C c;
        A* pb = &b;
        A* pc = &c;
        *pb = *pb;
        *pb = *pc;
        *pc = *pb;
        *pc = *pc;
    }

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    phantomotap - was your post directed at anon or me?

    edit: I don't see what problem you're trying to explain better.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Oh you mean because the = operator doesn't actually assign anything?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM