implicit conversion through constructor

Discussion in 'C++' started by Jess, Jun 14, 2007.

  1. Jess

    Jess Guest

    Hello,

    I can perform implicit conversion through constructor, like

    class A{
    public:
    A(int x):a(x){};
    int a;
    };

    int main(){
    A a = 10;
    return 0;
    }

    However, if I have

    class A{
    public:
    A(int x, int y):a(x),b(y){};
    int a;
    int b;
    };

    Then the compiler doesn't allow me to convert:

    int main(){
    A a = (10,20);
    return 0;
    }

    How can I perform implicit conversion through constructor with
    multiple arguments?

    Many thanks,
    Jess
     
    Jess, Jun 14, 2007
    #1
    1. Advertising

  2. Jess

    Marcus Kwok Guest

    Jess <> wrote:
    > However, if I have
    >
    > class A{
    > public:
    > A(int x, int y):a(x),b(y){};
    > int a;
    > int b;
    > };
    >
    > Then the compiler doesn't allow me to convert:
    >
    > int main(){
    > A a = (10,20);
    > return 0;
    > }
    >
    > How can I perform implicit conversion through constructor with
    > multiple arguments?


    You can not. If the constructor needs more than one argument, you must
    be explicit, like:

    A a = A(10, 20);

    or

    A a(10, 20);

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
     
    Marcus Kwok, Jun 14, 2007
    #2
    1. Advertising

  3. Jess

    Gavin Deane Guest

    On 14 Jun, 14:36, Jess <> wrote:
    > Hello,
    >
    > I can perform implicit conversion through constructor, like
    >
    > class A{
    > public:
    > A(int x):a(x){};
    > int a;
    >
    > };
    >
    > int main(){
    > A a = 10;
    > return 0;
    >
    > }


    Your constructor with the signature A(int x) is saying "an object of
    type A can be implicitly created from an object or expression of type
    int". Implicit conversions from one type to another exist for the
    built-in types in the language, and this feature allows you to create
    your own types that have the same behaviour.

    > However, if I have
    >
    > class A{
    > public:
    > A(int x, int y):a(x),b(y){};
    > int a;
    > int b;
    >
    > };
    >
    > Then the compiler doesn't allow me to convert:
    >
    > int main(){
    > A a = (10,20);


    This statement tries to use the expression (10,20) to initialise a.
    The expression (10,20) has the type int and evaluates to the value 20
    (the comma operator evaluates its first argument, 10, and discards it
    then evaluates its second argument, 20, and returns that value. The
    statement won't compile and the error you get should say something
    along the lines of "no constructor available that takes a single int".

    Try this code with your original class A definition where the
    constructor was A(int x) and it will compile.

    > return 0;
    >
    > }
    >
    > How can I perform implicit conversion through constructor with
    > multiple arguments?


    You can't. Implicit conversion is about converting from one type to
    another type. If you have multiple arguments to the constructor there
    is no one type to implicitly convert from.

    Gavin Deane
     
    Gavin Deane, Jun 14, 2007
    #3
  4. Jess wrote:

    > Hello,
    >
    > I can perform implicit conversion through constructor, like
    >
    > class A{
    > public:
    > A(int x):a(x){};
    > int a;
    > };
    >
    > int main(){
    > A a = 10;
    > return 0;
    > }
    >
    > However, if I have
    >
    > class A{
    > public:
    > A(int x, int y):a(x),b(y){};
    > int a;
    > int b;
    > };
    >
    > Then the compiler doesn't allow me to convert:
    >
    > int main(){
    > A a = (10,20);


    First of all, this should mean that the language would need some notion of
    tuples. C++ doesn't support that. This syntax, however would use the comma
    operator on 10 and 20, which throws away 10 and returns 20.

    This example is rather uninteresting, too.

    A a /*=*/ (10, 20);

    would compile, and has almost the same syntax.

    > return 0;
    > }
    >
    > How can I perform implicit conversion through constructor with
    > multiple arguments?


    You can't. And why would you? The meaning of implicit is that it is
    implicit, and even if you want some syntactic sugar, your example would be
    mostly explicit. Look at this:

    void foo(A);
    ....
    int value = some expression;
    foo(value);

    Here, the argument value is implicitly converted to an A object (assuming
    the first definition of A). Should your constructor mean that

    foo(value,value);

    would call the same function with both arguments converted to (assuming the
    latter A) A? Or would you have to write foo((value, value)), which would be
    more explicit.

    If you want to pass more than one value per parameter, or return more than
    one value, you should look at the tuple library from boost.org.

    --
    rbh
     
    Robert Bauck Hamar, Jun 14, 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. sb
    Replies:
    10
    Views:
    4,610
    Andre Heinen
    Feb 25, 2004
  2. flopbucket
    Replies:
    1
    Views:
    498
    Tom Widmer
    Jun 26, 2006
  3. Alex Vinokur
    Replies:
    9
    Views:
    413
    Thomas J. Gritzan
    Aug 10, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,245
  5. ingoweiss
    Replies:
    4
    Views:
    210
    Julian Turner
    May 12, 2006
Loading...

Share This Page