Use of virtual

This is a discussion on Use of virtual within the C++ Programming forums, part of the General Programming Boards category; Hallo, Is it possible for a virtual function to take different arguments? Like this: Code: class Vehicle { public: Object(void); ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Use of virtual

    Hallo,

    Is it possible for a virtual function to take different arguments? Like this:
    Code:
    class Vehicle
    {
    	public:
    		Object(void);
    		~Object(void);
    
    		virtual void update() = 0;
    		virtual void draw() = 0;
    };
    
    class Truck : public Vehicle
    {
    	public:
    		Player();
    
    		void update(int a, int b);
    		void draw();		
    };
    Thanks

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    No. A function taking different arguments is a different function.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Wouldn't that be a little confusing if you did that though? The base class is essentially a contract between the code and the developer saying all derived classes will look at least like this.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I though it would be useful to do something like that. But after thinking about it for a while I guess it would be stupid if that worked in some way. It would break the whole concept behind virtual (i think)

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The idea of virtual is that you can manipulate objects through a base pointer without caring what the concrete type is. If virtual functions would take different arguments in different classes, you'd have to first find out what type the pointer is actually pointing to in order to be able to call the function with correct parameters. If I'm not mistaken, you can already do that with non-virtual functions just fine.
    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
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is of course possible to have TWO DIFFERENT virtual functions that are different only in the arguments they take, e.g.
    Code:
    struct coord
    {
        int x;
        int y;
    }
    class base
    {
       public:
          virtual void vfunc(int x, int y);
    };
    
    class derived: public base
    {
       public:
          virtual void vfunc(int x, int y);
          virtual void vfunc(coord &c);
    }
    
    void base::vfunc(int x, int y)
    {
    ... code here 
    }
    
    void base::vfunc(coord &c)
    {
       vfunc(c.x, c.y);
    }
    
    void derived::vfunc(int x, int y)
    {
    ... code here 
    }
    Note that since the base::vfunc(coord &) calls the virtual vfunc(int, int) form, it will automatically call the derived::vfunv(int, int).

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

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    matsp, your a clever man.

    Thanks

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Is it bad to have a derived class holding and instance of an other derived class from the same base? I had an argument with a friend about it.

    Here is what I am doing

    Code:
    class Base
    {
        public:
            Base();
            virtual void foo();
    }
    
    class DerivedA : public Base
    {
        public:
            DerivedA();
            void foo();
    }
    
    class DerivedB : public Base
    {
        public:
            DerivedB();
            void foo();
    
        private:
            DerivedA *a;
    }
    Is that OK?

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Is it bad to have a derived class holding and instance of an other derived class from the same base?
    No.

    I had an argument with a friend about it.
    What was the argument about?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I see no problem with that in itself.

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

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Quote Originally Posted by laserlight View Post
    No.


    What was the argument about?
    He said it was bad design while I said it was ok. Going to force him to buy me a beer now.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual Box
    By ssharish2005 in forum Tech Board
    Replies: 3
    Last Post: 02-12-2009, 04:08 AM
  2. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  3. Program with Shapes using Virtual Functions
    By goron350 in forum C++ Programming
    Replies: 12
    Last Post: 07-17-2005, 01:42 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 10:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21