Best of both worlds yet?

Discussion in 'C++' started by Jim Langston, Sep 25, 2010.

  1. Jim Langston

    Jim Langston Guest

    I am using ms c++ express 10 which has some early c++0x implementations I
    understand. What I would like to do is have the best of both worlds with
    POD and classes for initialization. For example, with a POD I can do:
    Foo bar[] = { 12, 23, 44 };
    but, with a class with a constructor I can say:
    somefunc( bar( 12, 23, 44 ) );
    which will create a temporary bar and pass that which works as long as it's
    a constant parameter.

    I can't seem to be able to do both at the same time however. If I create a
    constructor the first form doesn't produce the expected object. (I tried
    with 1, 2, 3 and displayed the first value and it was 0.

    Is there anythign in C++0x that helps me here?
     
    Jim Langston, Sep 25, 2010
    #1
    1. Advertising

  2. * Jim Langston, on 25.09.2010 09:15:
    > I am using ms c++ express 10 which has some early c++0x implementations I
    > understand. What I would like to do is have the best of both worlds with POD and
    > classes for initialization. For example, with a POD I can do:
    > Foo bar[] = { 12, 23, 44 };
    > but, with a class with a constructor I can say:
    > somefunc( bar( 12, 23, 44 ) );
    > which will create a temporary bar and pass that which works as long as it's a
    > constant parameter.
    >
    > I can't seem to be able to do both at the same time however. If I create a
    > constructor the first form doesn't produce the expected object. (I tried with 1,
    > 2, 3 and displayed the first value and it was 0.
    >
    > Is there anythign in C++0x that helps me here?


    Yes, C++ has support for curly braces construction but (1) it's a bit
    under-specified in that the lifetime of the initializer is (at least to me) not
    quite clear, and (2) it's not supported by Visual C++ 10.0.


    Cheers,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
     
    Alf P. Steinbach /Usenet, Sep 25, 2010
    #2
    1. Advertising

  3. Jim Langston

    SG Guest

    On 25 Sep., 09:15, Jim Langston wrote:
    > I am using ms c++ express 10 which has some early c++0x implementations I
    > understand.  What I would like to do is have the best of both worlds with
    > POD and classes for initialization.  For example, with a POD I can do:
    >
    > Foo bar[] = { 12, 23, 44 };


    This defines an array called bar of type Foo[3], provided that Foo is
    an aggregate which can be initialized with an integer or has an non-
    explicit constructor that takes a parameter T where int is convertible
    to T.

    > but, with a class with a constructor I can say:
    >
    > somefunc( bar( 12, 23, 44 ) );


    What kind of type is "bar"? Did you mean to write "Foo(12,23,44)"?
    How is somefunc declared? What is this supposed to do?

    > which will create a temporary bar and pass that which works as long as it's
    > a constant parameter.
    >
    > I can't seem to be able to  do both at the same time however.


    Which is what exactly?

    > If I create a
    > constructor the first form doesn't produce the expected object.


    By "first" you mean the ARRAY bar of type Foo[3]?
    If you add a constructor to Foo it doesn't necessarily make the first
    syntax invalid.

    > (I tried
    > with 1, 2, 3 and displayed the first value and it was 0.
    >
    > Is there anythign in C++0x that helps me here?


    I have no idea what you are talking about. What is it that you want?

    Cheers!
    SG
     
    SG, Sep 25, 2010
    #3
  4. Jim Langston

    Jim Langston Guest

    "SG" <> wrote in message
    news:...
    On 25 Sep., 09:15, Jim Langston wrote:
    >> I am using ms c++ express 10 which has some early c++0x implementations I
    >> understand. What I would like to do is have the best of both worlds with
    >> POD and classes for initialization. For example, with a POD I can do:
    >>
    >> Foo bar[] = { 12, 23, 44 };

    >
    >This defines an array called bar of type Foo[3], provided that Foo is
    >an aggregate which can be initialized with an integer or has an non-
    >explicit constructor that takes a parameter T where int is convertible
    >to T.
    >
    >> but, with a class with a constructor I can say:
    >>
    >> somefunc( bar( 12, 23, 44 ) );

    >
    >What kind of type is "bar"? Did you mean to write "Foo(12,23,44)"?
    >How is somefunc declared? What is this supposed to do?
    >
    >> which will create a temporary bar and pass that which works as long as
    >> it's
    >> a constant parameter.
    >>
    >> I can't seem to be able to do both at the same time however.

    >
    >Which is what exactly?
    >
    >> If I create a
    >> constructor the first form doesn't produce the expected object.

    >
    >By "first" you mean the ARRAY bar of type Foo[3]?
    >If you add a constructor to Foo it doesn't necessarily make the first
    >syntax invalid.
    >
    >> (I tried
    >> with 1, 2, 3 and displayed the first value and it was 0.
    >>
    >> Is there anythign in C++0x that helps me here?

    >
    >I have no idea what you are talking about. What is it that you want?


    Simple, I would like this to compile and work as expected, which it doesn't
    in msvc++ 10.

    #include <iostream>

    struct Foo {
    float x, y, z;
    };

    struct Bar {
    float x, y, z;
    Bar( float x = 0.0f, float y = 0.0f, float z = 0.0f ): x(x), y(y), z(z) {
    }
    };

    void Bat( const Foo& f ) {
    }

    void Bah( const Bar& b ) {
    }

    int main() {
    Foo f = { 1.0f, 2.0f, 3.0f };
    Bar b = { 1.0f, 2.0f, 3.0f }; // Error, non-aggregates cannot be
    initialized with initializer list
    Bat( Foo( 1.0f, 2.0f, 3.0f ) ); // Error, Foo::Foo : no overloaded
    function takes 3 arguments
    Bah( Bar( 1.0f, 2.0f, 3.0f ) );
    std::cout << f.x << " " << f.y << " " << f.z << "\n";
    std::cout << b.x << " " << b.y << " " << b.z << "\n";
    }

    Alf has stated that msvc++ 10 doesn't support this yet.

    Thanks.

    Jim Langston
     
    Jim Langston, Sep 25, 2010
    #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. Daniel T.
    Replies:
    2
    Views:
    346
    Daniel T.
    Oct 19, 2004
  2. Bruce Grubb
    Replies:
    1
    Views:
    497
    Stephen Adams
    May 23, 2006
  3. Replies:
    11
    Views:
    626
    Tor Iver Wilhelmsen
    Oct 13, 2006
  4. andrew
    Replies:
    0
    Views:
    294
    andrew
    Jul 3, 2005
  5. J2ee Enterprise Matrix
    Replies:
    0
    Views:
    350
    J2ee Enterprise Matrix
    Jan 31, 2008
Loading...

Share This Page