Can we use a virtual function in the class in which the virtual function is defined?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CPP freshman
    New Member
    • May 2012
    • 15

    Can we use a virtual function in the class in which the virtual function is defined?

    Code:
    class Parent{
      public:
        virtual int width();
        virtual int height();
        int area(){return width()*height();};
        //[B]Virtual functions are used here to define another function, but this produces an error. [/B] 
    //[B]Is is possible to do something like this? [/B]
    };
    
    class Child: public Parent{
      int w;
      int h;
      public:
        Child(int a,int b){w=a;h=b;};
        int width(){return w;}
        int height(){return h;}
    };
    Last edited by zmbd; Dec 27 '12, 10:46 PM. Reason: [Z{Please use the <CODE/> button to format posted code/html/sql}{Please note, normally posted code w/o explanation or question within the body of the post might be deleted.]
  • thepiedpiper
    New Member
    • May 2010
    • 4

    #2
    Hi,

    You have a semicolon at the end of this line:

    Code:
    int area(){return width()*height();};
    You don't need the last semicolon here. Remove it. The problem is with this semicolon and not with your virtual functions.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      You may not understand the purpose of a virtual function.

      A virtual function is used when the derived portion of an object is accessed from a pointer to the base portion of the object. This is what allows polymorphism in C++.

      Therefore, you are not allowed to call a virtual function using an object of the class that defines it.

      You can, however, call a base class virtual function from a member function in a derved object:

      Code:
      class Base
      {
        public:
          virtual void Funct();
      };
      class Derived :public Base
      {
         public:
           virtual void Funct();
      };
      void Derived::Funct()
      {
          Base::Funct();  //OK
      }

      Comment

      • CPP freshman
        New Member
        • May 2012
        • 15

        #4
        Thanks for your reply, but I already knew what you just said.

        Comment

        • CPP freshman
          New Member
          • May 2012
          • 15

          #5
          I don't think this is the problem.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            It may be. You are calling derived class functions in the base class.

            virtual functions in the base class define an interface to the hierarchy. You make your calls in the derived class. By placing these functions in the base class you force every derived class to support them. Some derived classes may not have length or width so keep the derived functions with the derived class.

            This is a place to review the Visitor design pattern. This allows you to acquire a Child* from an object pointed at by a Parent*. This then allows you to call Child functions on Child objects that only have Parent*.

            Read this: http://bytes.com/topic/c/insights/67...tterns-visitor

            Comment

            • CPP freshman
              New Member
              • May 2012
              • 15

              #7
              I think I am going to use a template function to do this.
              Code:
              class Parent{
                public:
                  virtual int width();
                  virtual int height();
                 
              };
               
              class Child: public Parent{
                int w;
                int h;
                public:
                  Child(int a,int b){w=a;h=b;};
                  int width(){return w;}
                  int height(){return h;}
              };
              template <class T> int area(T){return T.width()*T.height();};
              But it's not exactly what I wanted though.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                You need code like this:

                Code:
                Parent* p = new Child(3,4);
                
                Child* c = p->VisitChild();
                
                cout << c->area();
                and not a template.

                Comment

                • CPP freshman
                  New Member
                  • May 2012
                  • 15

                  #9
                  Code:
                  Parent* p = new Child(3,4);
                   
                  Child* c = p->VisitChild();[B]//Could you explain in a little bit more detail please? VisitChild() appears not defined. [/B]
                   
                  cout << c->area();

                  Comment

                  • CPP freshman
                    New Member
                    • May 2012
                    • 15

                    #10
                    changed, and worked, though still not exactly what I wanted.
                    Code:
                    lass Parent{
                      public:
                        virtual int width();
                        virtual int height();
                     
                    };
                     
                    class Child: public Parent{
                      int w;
                      int h;
                      public:
                        Child(int a,int b){w=a;h=b;};
                        int width(){return w;}
                        int height(){return h;}
                    };
                    int area(Parent p){return p.width()*p.height();};

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      I meant that as a directional example and not a compilable one. Sorry.

                      A working example is in the article I gave you a link to my Post # 8 above.

                      Comment

                      Working...