virtual function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hugheslin@hotmail.com

    #1

    virtual function

    Hi,

    Please consider the following classes:

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    class Shape
    {
    ......
    ......
    public:
    virtual void draw(CDC & aDC, COLORREF color, COLORREF fcolor, int
    width, BOOL Filled = false) = 0;
    };

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    class Line : public Shape
    {
    ......
    ......
    public:
    void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width,
    BOOL Filled = false);
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    class ellipse : public Shape
    {
    ......
    ......
    public:
    void draw(CDC & dc, COLORREF color, COLORREF fcolor, int width,
    BOOL Filled = false);
    };
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    My question is:

    If I declared the member function "draw( )" inside the classes and
    implemented the definitions of "draw( )" outside the classes, then
    there will be an error messsage when compiling :

    "redefiniti on of default parameter : parameter 5"

    However, if I implemented all the definitions of "draw( )" inside the
    classes, then the code can pass the compilation.

    Would anybody please let me know what the issue is? Thakns a lot.

  • Gavin Deane

    #2
    Re: virtual function


    hugheslin@hotma il.com wrote:[color=blue]
    > Hi,
    >
    > Please consider the following classes:
    >
    > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    > class Shape
    > {
    > .....
    > .....
    > public:
    > virtual void draw(CDC & aDC, COLORREF color, COLORREF fcolor, int
    > width, BOOL Filled = false) = 0;
    > };
    >
    > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    >
    > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    > class Line : public Shape
    > {
    > .....
    > .....
    > public:
    > void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width,
    > BOOL Filled = false);
    > }
    > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    >
    > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    > class ellipse : public Shape
    > {
    > .....
    > .....
    > public:
    > void draw(CDC & dc, COLORREF color, COLORREF fcolor, int width,
    > BOOL Filled = false);
    > };
    > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    >
    > My question is:
    >
    > If I declared the member function "draw( )" inside the classes and
    > implemented the definitions of "draw( )" outside the classes, then
    > there will be an error messsage when compiling :
    >
    > "redefiniti on of default parameter : parameter 5"
    >
    > However, if I implemented all the definitions of "draw( )" inside the
    > classes, then the code can pass the compilation.[/color]

    A much simpler example of the same problem is the following

    // Function declaration
    void foo(int i = 42);

    int main()
    {
    return 0;
    }

    // Function definition
    void foo(int i = 42) {}

    You are not allowed to define the default parameter in two places. The
    solution is either

    // Function declaration and definition
    void foo(int i = 42) {}

    int main()
    {
    return 0;
    }

    which is the equivalent of you defining your draw functions inside your
    classes, or

    // Function declaration
    void foo(int i = 42) {}

    int main()
    {
    return 0;
    }

    // Function definition
    // Note default parameter not redefined
    void foo(int i) {}

    Gavin Deane

    Comment

    • Neelesh Bodas

      #3
      Re: virtual function


      hugheslin@hotma il.com wrote:[color=blue]
      > Hi,
      >
      > Please consider the following classes:
      >[/color]
      [color=blue]
      > class Shape[/color]
      {> public:[color=blue]
      > virtual void draw(CDC & aDC, COLORREF color, COLORREF fcolor, int
      > width, BOOL Filled = false) = 0;
      > };
      > class Line : public Shape
      > {
      > public:
      > void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width,
      > BOOL Filled = false);
      > }[/color]
      [color=blue]
      > My question is:
      >
      > If I declared the member function "draw( )" inside the classes and
      > implemented the definitions of "draw( )" outside the classes, then
      > there will be an error messsage when compiling :
      >
      > "redefiniti on of default parameter : parameter 5"[/color]

      Actually, your question has nothing to with "virtual functions". C++
      doesnot allow redefinition of default arguments of functions. So once
      they are defined in the function prototype, you should not specify them
      again in the function definition.
      <quote>
      8.3.6p4 : A default argument shall not be redefined by a later
      declaration (not even to the same value).
      </quote>

      Comment

      • Fiesta

        #4
        Re: virtual function

        Hi Gavin,

        Thanks a lot for your suggestion and it did help. Thanks again.


        Gavin Deane wrote:[color=blue][color=green]
        > > Hi,
        > >
        > > Please consider the following classes:
        > >
        > > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        > > class Shape
        > > {
        > > .....
        > > .....
        > > public:
        > > virtual void draw(CDC & aDC, COLORREF color, COLORREF fcolor, int
        > > width, BOOL Filled = false) = 0;
        > > };
        > >
        > > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        > >
        > > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        > > class Line : public Shape
        > > {
        > > .....
        > > .....
        > > public:
        > > void draw(CDC &dc, COLORREF color, COLORREF fcolor, int width,
        > > BOOL Filled = false);
        > > }
        > > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        > >
        > > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        > > class ellipse : public Shape
        > > {
        > > .....
        > > .....
        > > public:
        > > void draw(CDC & dc, COLORREF color, COLORREF fcolor, int width,
        > > BOOL Filled = false);
        > > };
        > > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        > >
        > > My question is:
        > >
        > > If I declared the member function "draw( )" inside the classes and
        > > implemented the definitions of "draw( )" outside the classes, then
        > > there will be an error messsage when compiling :
        > >
        > > "redefiniti on of default parameter : parameter 5"
        > >
        > > However, if I implemented all the definitions of "draw( )" inside the
        > > classes, then the code can pass the compilation.[/color]
        >
        > A much simpler example of the same problem is the following
        >
        > // Function declaration
        > void foo(int i = 42);
        >
        > int main()
        > {
        > return 0;
        > }
        >
        > // Function definition
        > void foo(int i = 42) {}
        >
        > You are not allowed to define the default parameter in two places. The
        > solution is either
        >
        > // Function declaration and definition
        > void foo(int i = 42) {}
        >
        > int main()
        > {
        > return 0;
        > }
        >
        > which is the equivalent of you defining your draw functions inside your
        > classes, or
        >
        > // Function declaration
        > void foo(int i = 42) {}
        >
        > int main()
        > {
        > return 0;
        > }
        >
        > // Function definition
        > // Note default parameter not redefined
        > void foo(int i) {}
        >
        > Gavin Deane[/color]

        Comment

        Working...