Reply by donhamilton2002 October 6, 20102010-10-06
--- In m..., aliko wrote:
>
> Microsoft Visual C++ 2010 works fine with this code. Also

I have seen VC++ 2005 and 2008 allow illegal code to compile.

I think its M$ way to "helping" coders make mistakes, but just let them pass.

M$ is here to help you, right or wrong.

don

Beginning Microcontrollers with the MSP430

Reply by aliko October 6, 20102010-10-06
06.10.2010 16:27, davidbrown563 пишет:
>
> I'm not sure whether your code is valid C++ or not. However, it's not going to work as you are hoping.
>
> By using temporaries in "rng *r[] = {&rng(9),&rng(0)};" the rng objects themselves will only exist with the scope of the initialiser - they will disappear once the r[] array is set up. So the addresses in r[] will no longer point to rng objects.
>
You are pretty right! May be it a valid C++. Nevertheless it is not
works as I expected and GCC is warning about it!

Reply by davidbrown563 October 6, 20102010-10-06
--- In m..., aliko wrote:
>
> 06.10.2010 14:09, Anders Lindgren пишет:
> > Hi Aliko!
> >
> > It gives you an error since it is not valid C++ code.
> >
> > You can't create objects and immediately take the address of them.
> >
> > Here are a few ways to rewrite the code to work. If you really want an
> > array of pointers, you can write:
> >
> > // ---------
> > rng rng9(9);
> > rng rng0(0);
> >
> > rng *r[] = {&rng9,&rng0};
> > // ---------
> >
> > Alternatively, if you can live with an array of objects, you could write:
> >
> > // ---------
> > rng r[] = {rng(9), rng(0)};
> > // ---------
> >
> > Sincerely,
> > -- Anders Lindgren, IAR Systems
>
> Thank you for promptest response!
> Thats the way I already done this. But I thought that I can do job not
> using unneeded declarations but using anonymous objects. Somehow
> Microsoft Visual C++ 2010 works fine with this code. Also msp430-gcc
> compiles it as well as mingw32-gcc but seems it not work correctly with
> mingw32-gcc, the unnamed objects because the unnamed objects are temporary.
>

I'm not sure whether your code is valid C++ or not. However, it's not going to work as you are hoping.

By using temporaries in "rng *r[] = {&rng(9), &rng(0)};" the rng objects themselves will only exist with the scope of the initialiser - they will disappear once the r[] array is set up. So the addresses in r[] will no longer point to rng objects.

Reply by aliko October 6, 20102010-10-06
06.10.2010 14:09, Anders Lindgren пишет:
> Hi Aliko!
>
> It gives you an error since it is not valid C++ code.
>
> You can't create objects and immediately take the address of them.
>
> Here are a few ways to rewrite the code to work. If you really want an
> array of pointers, you can write:
>
> // ---------
> rng rng9(9);
> rng rng0(0);
>
> rng *r[] = {&rng9,&rng0};
> // ---------
>
> Alternatively, if you can live with an array of objects, you could write:
>
> // ---------
> rng r[] = {rng(9), rng(0)};
> // ---------
>
> Sincerely,
> -- Anders Lindgren, IAR Systems

Thank you for promptest response!
Thats the way I already done this. But I thought that I can do job not
using unneeded declarations but using anonymous objects. Somehow
Microsoft Visual C++ 2010 works fine with this code. Also msp430-gcc
compiles it as well as mingw32-gcc but seems it not work correctly with
mingw32-gcc, the unnamed objects because the unnamed objects are temporary.

Reply by Anders Lindgren October 6, 20102010-10-06
On 2010-10-06 09:04, aliko wrote:
> Why this simple code not working with IAR C/C++ Compiler for MSP430 4.21.2
>
> struct rng
> {
> rng(int n) : i(n) {};
> int i;
> };
> rng *r[] = {&rng(9), &rng(0)};
>
> It gives Error[Pe158]: expression must be an lvalue or a function
> designator

Hi Aliko!

It gives you an error since it is not valid C++ code.

You can't create objects and immediately take the address of them.

Here are a few ways to rewrite the code to work. If you really want an
array of pointers, you can write:

// ---------
rng rng9(9);
rng rng0(0);

rng *r[] = {&rng9, &rng0};
// ---------

Alternatively, if you can live with an array of objects, you could write:

// ---------
rng r[] = {rng(9), rng(0)};
// ---------

Sincerely,
-- Anders Lindgren, IAR Systems
--
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.

Reply by aliko October 6, 20102010-10-06
Why this simple code not working with IAR C/C++ Compiler for MSP430 4.21.2

struct rng
{
rng(int n) : i(n) {};
int i;
};
rng *r[] = {&rng(9), &rng(0)};

It gives Error[Pe158]: expression must be an lvalue or a function
designator