Best of both worlds yet?

J

Jim Langston

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?
 
A

Alf P. Steinbach /Usenet

* 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
 
S

SG

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
 
J

Jim Langston

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top