ctor ambiguity

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Apr 19, 2007.

  1. , India

    , India Guest

    I have tried the following in VC++ 2005 Express Edition and g++ in
    Linux.

    Consider the class

    class my_complex
    {
    public:
    my_complex(double r, double i = 10.0) : re(r), im(i) { }
    my_complex(double r) : re(r), im(0) { }
    private:
    double re;
    double im;
    };

    If I create an object like
    my_complex c(100.0);
    the compiler generates error as expected, due to ambiguity in the
    ctors.

    However if I do not create an object of my_complex, the compiler does
    not
    generate any warning or error. Why doesn't the compiler detect the
    ambiguity
    when an object of type my_complex is not created ? kindly explain.
     
    , India, Apr 19, 2007
    #1
    1. Advertising

  2. * , India:
    > I have tried the following in VC++ 2005 Express Edition and g++ in
    > Linux.
    >
    > Consider the class
    >
    > class my_complex
    > {
    > public:
    > my_complex(double r, double i = 10.0) : re(r), im(i) { }
    > my_complex(double r) : re(r), im(0) { }
    > private:
    > double re;
    > double im;
    > };
    >
    > If I create an object like
    > my_complex c(100.0);
    > the compiler generates error as expected, due to ambiguity in the
    > ctors.


    Decide whether you want the default for im to be 0.0 or 10.0.


    > However if I do not create an object of my_complex, the compiler does
    > not
    > generate any warning or error. Why doesn't the compiler detect the
    > ambiguity
    > when an object of type my_complex is not created ?


    There's no ambiguity if you're not trying to call one of those constructors.

    The ambiguity is not caused by creating an object: you can define some
    other constructor and create an object using that constructor; e.g., you
    should be able to do

    my_complex heh = heh;

    using the automatically generated copy constructor (which is an
    interesting case of circumventing the C++ initialization guarantee).

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Apr 19, 2007
    #2
    1. Advertising

  3. , India

    Zeppe Guest

    , India wrote:

    > However if I do not create an object of my_complex, the compiler does
    > not
    > generate any warning or error. Why doesn't the compiler detect the
    > ambiguity
    > when an object of type my_complex is not created ? kindly explain.
    >

    Oh, well, strictly speaking there is no ambiguity as soon as you ask for
    a function that has multiple candidates. I mean, if you create an object

    my_complex c(1.0,1.0);

    it should be fine. You should wonder why the compiler shouldn't warn you
    about the fact that you can't solve any more the function call if you
    give just one argument.

    There is indeed a general way to solve the ambiguity: consider this
    simpler code:

    int f()
    {
    return 0;
    }

    int f(int i = 0)
    {
    return i;
    }

    int main()
    {
    int (*fp)(void) = f;
    fp();
    return 0;
    }


    Bye!

    Zeppe
     
    Zeppe, Apr 19, 2007
    #3
  4. , India

    Ron Natalie Guest

    , India wrote:
    > I have tried the following in VC++ 2005 Express Edition and g++ in
    > Linux.
    >
    > Consider the class
    >
    > class my_complex
    > {
    > public:
    > my_complex(double r, double i = 10.0) : re(r), im(i) { }
    > my_complex(double r) : re(r), im(0) { }
    > private:
    > double re;
    > double im;
    > };
    >
    > If I create an object like
    > my_complex c(100.0);
    > the compiler generates error as expected, due to ambiguity in the
    > ctors.
    >
    > However if I do not create an object of my_complex, the compiler does
    > not
    > generate any warning or error. Why doesn't the compiler detect the
    > ambiguity
    > when an object of type my_complex is not created ? kindly explain.
    >


    There's no ambiguity in the case of:
    my_complex(5.0, 5.0)
    for example.

    The compiler might notice that the one arg'd constructor can't be
    unambiguously reached, but it's not required, and frankly if your
    program never creates an object like my_complex(100.0), it would
    not be an issue.
     
    Ron Natalie, Apr 19, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apricot
    Replies:
    4
    Views:
    556
    velthuijsen
    Apr 16, 2004
  2. NVH
    Replies:
    8
    Views:
    516
    mlimber
    Jul 6, 2006
  3. Grizlyk
    Replies:
    8
    Views:
    506
    Grizlyk
    Nov 29, 2006
  4. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    427
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
  5. Anonymous
    Replies:
    2
    Views:
    388
    Victor Bazarov
    Aug 28, 2007
Loading...

Share This Page