tags:

views:

107

answers:

2
+2  Q: 

pointer assignment

I have the following template structure:

template <typename scalar_type>
struct postc_params{
    scalar_type delta; 
    unsigned int time_horizon; 
    matrix_math::matrix<scalar_type> dynamics;
    boost::shared_ptr<continuous_set> invariant_set_ptr;
    boost::shared_ptr<continuous_set> input_set_ptr;
    boost::shared_ptr<continuous_set> initial_set_ptr;
};

Now, I have a templated class with a private member of the above structure type

template <typename scalar_type>
class A{
....
private:
....
postc_params<scalar_type> my_postc;
};

Inside a member function definition of class A, I have the following line of code:

my_postc.initial_set_ptr = my_postc.initial_set_ptr->transform(some_obj);

transform function returns a pointer of type

boost::shared_ptr<continuous_set>

With this code, I have the following error:

passing 'const boost::shared_ptr' as 'this' argument of 'boost::shared_ptr< >& boost::shared_ptr< >::operator= (const boost::shared_ptr&) [with Y = const continuous::continuous_set, T = continuous::continuous_set]' discards qualifiers

Can anyone help me out with the cause?

+1  A: 

Is the member function in A const?

If I am reading your code right, you are trying to change a member of a class from a const member function which is not allowed. Either remove the const from the member function or make the member mutable.

So,

mutable postc_params<scalar_type> my_postc;

However, I would take care with this method. Maybe reevaluate why the method that is changing my_postc is const. Either it should not be const or it should not be changing my_postc.

Jesse
Ah..yes it is indeed const.
rayimag
To throw in my 2 cents, this is not what I would consider a "good" place to use `mutable`.
rlbond
+1  A: 

you are trying to assign to a const pointer as per the error message: "passing 'const boost::shared_ptr' as 'this' argument"

the member function you mention is surely const hence the error

you should rather reconsider your design than throw mutable here and there in your code.

jlru