CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2001
    Posts
    4

    Automatic casting of in-parameter

    Hi,

    I have two classes: class A and class B. class B inherits from A and both have a public function named func(). func() in class A takes an int as parameter, and class B takes a double.

    After creating an instance of class B, I wish to call func(int) using the class B object. But how can I do this? When calling B objB.func(int i=40)

    the in parameter is automatically casted to a double and func(double) in class B is run.

    Can someone explain this "unexpected" behaviour? I have no compiler warnings/errors.

    Here is my code:


    include <stdio.h>

    //! class A with int as parameter to its func()
    class A
    {
    public:
    void func(int param){printf("A::func(int)\n");};
    };


    //! class B with double as parameter to its func()
    class B : public A
    {
    public:
    void func(double param){printf("B::func(double)\n");};
    };



    //! Test app
    int main(void)
    {
    double dNum = 3.14;
    int iNum = 1;

    A objA;
    B objB;

    objA.func(iNum);
    objB.func(dNum);
    objB.func(iNum); //parameter is automatically casted to double!

    return 0;
    }


    // PROGRAM OUTPUT:
    //
    // A::func(int)
    // B::func(double)
    // B::func(double)





    Kjetil



  2. #2
    Join Date
    Feb 2001
    Location
    Sydney, Australia
    Posts
    1,909

    Re: Automatic casting of in-parameter

    Hi ! You have to use using declaration to make A::func(int) visible inside class B:

    so the code will be looks like:


    include <stdio.h>

    //! class A with int as parameter to its func()
    class A
    {
    public:
    void func(int param){printf("A::func(int)\n");};
    };


    //! class B with double as parameter to its func()
    class B : public A
    {
    public:
    using A::func;//using declaration
    void func(double param){printf("B::func(double)\n");};
    };



    //! Test app
    int main(void)
    {
    double dNum = 3.14;
    int iNum = 1;

    A objA;
    B objB;

    objA.func(iNum);
    objB.func(dNum);
    objB.func(iNum); //parameter is automatically casted to double!

    return 0;
    }






    Please - rate answer if it helped you
    It gives me inspiration when I see myself in the top list =)

    Best regards,

    -----------
    Igor Soukhov (Brainbench/Tekmetrics ID:50759)
    [email protected] | ICQ:57404554 | http://soukhov.com

    Member of Russian Software Developer Network http://rsdn.ru
    Best regards,
    Igor Sukhov

    www.sukhov.net

  3. #3
    Join Date
    Oct 2001
    Location
    Israel
    Posts
    16

    Re: Automatic casting of in-parameter

    You need to declare func() as a virtual function.


  4. #4
    Join Date
    Oct 2001
    Posts
    4

    Re: Automatic casting of in-parameter

    Thank you for your reply. I've tested it and it works! :-)

    I read something about silent type conversion yesterday in one of Scott Meyers' books. I believe this was what that really happened in my example.


    Here is an extract:

    "C++ allows compilers to perform implicit conversions between types. In
    honor of its C heritage, for example, the language allows silent conversions from
    char to int and from short to double. This is why you can pass a short to a function
    that expects a double and still have the call succeed. The more frightening conversions
    in C — those that may lose information — are also present in C++, including conversion
    of int to short and double to (of all things) char."

    ...

    "You can't do anything about such conversions, because they're hard-coded into the language."


    Excellent book by the way ("Effective C++: 35 ways to improve your C++") .


    Regards,

    Kjetil


  5. #5
    Join Date
    Oct 2001
    Posts
    4

    Re: Automatic casting of in-parameter

    Thanks for your reply.

    But declaring func() as virtual functions doesn't work. I believe the reason to this is that the compiler performs a silent type conversion from int to double (you'll get similar results changing the func(int) function with a func(char) function).

    Please look Look at Igor Soukhov's post for a solution, and my next post for an extract from one of Scott Meyer's books discussing silent type conversion in C++.


    Regards,

    Kjetil


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured