Thread: Use of virtual

  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,895
    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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 05: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, 11: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