S
spacewrench
I'd like to give an array of pointers to a class instance, and let the
instance return references (either const or mutable) to the array
elements. However, when I return a const reference to one of the
pointers, I can still modify the pointed-to object through the pointer
(I just can't change to pointer to point to a different object).
Instead, I'd like to have the const-ness "trickle down," so that if I
return a const pointer, it's actually a const * const. (This is a
poor English description of the following code.) I'm using g++-4.3.2,
and it warns about returning a reference to a temporary in whynot().
I don't understand why a temporary must be created in this situation.
Is it a g++ anomaly, or is there a sneaky problem with what I'm trying
to do?
class Foo {
public:
void inspect( void ) const { }
void mutate( void ) { }
} ;
class Bar {
public:
Bar( Foo *a[] ) : arrayOfFoo( a ) { }
Foo * & elt ( unsigned n ) { return arrayOfFoo
[n]; }
Foo * const & elt ( unsigned n ) const { return arrayOfFoo
[n]; }
const Foo * const & whynot( unsigned n ) const { return arrayOfFoo
[n]; }
private:
Foo * * const arrayOfFoo;
} ;
int
main( int, char ** )
{
Foo *foo[10];
Bar bar( foo );
const Bar &cbar = bar;
bar.elt( 0 )->inspect( );
bar.elt( 1 )->mutate( );
cbar.elt( 2 )->inspect( );
cbar.elt( 3 )->mutate( ); // <--- Don't want to allow this
cbar.whynot( 4 )->inspect( );
cbar.whynot( 5 )->mutate( ); // <--- this gives error (yay!)
return 0;
}
Thanks,
dave m.
instance return references (either const or mutable) to the array
elements. However, when I return a const reference to one of the
pointers, I can still modify the pointed-to object through the pointer
(I just can't change to pointer to point to a different object).
Instead, I'd like to have the const-ness "trickle down," so that if I
return a const pointer, it's actually a const * const. (This is a
poor English description of the following code.) I'm using g++-4.3.2,
and it warns about returning a reference to a temporary in whynot().
I don't understand why a temporary must be created in this situation.
Is it a g++ anomaly, or is there a sneaky problem with what I'm trying
to do?
class Foo {
public:
void inspect( void ) const { }
void mutate( void ) { }
} ;
class Bar {
public:
Bar( Foo *a[] ) : arrayOfFoo( a ) { }
Foo * & elt ( unsigned n ) { return arrayOfFoo
[n]; }
Foo * const & elt ( unsigned n ) const { return arrayOfFoo
[n]; }
const Foo * const & whynot( unsigned n ) const { return arrayOfFoo
[n]; }
private:
Foo * * const arrayOfFoo;
} ;
int
main( int, char ** )
{
Foo *foo[10];
Bar bar( foo );
const Bar &cbar = bar;
bar.elt( 0 )->inspect( );
bar.elt( 1 )->mutate( );
cbar.elt( 2 )->inspect( );
cbar.elt( 3 )->mutate( ); // <--- Don't want to allow this
cbar.whynot( 4 )->inspect( );
cbar.whynot( 5 )->mutate( ); // <--- this gives error (yay!)
return 0;
}
Thanks,
dave m.