Cannot assign element that is a pointer to a templated class

My problem is that in my class "Widget" I have the following declaration:

MouseEvent* X;

      

In the member function, I initialize the pointer with the address in the usual way:

X = new MouseEvent;

      

Ok, this last line makes the compiler stop at:

error C2166: l-value specifies a const object

Okay, MouseEvent is declared as a typedef for simplicity:

typedef Event__2<void, Widget&, const MouseEventArgs&> MouseEvent;

      

And Event__2, as you can imagine: (main structure shown):

template <typename return_type, typename arg1_T, typename arg2_T>
class Event__2
{
     ...
};

      

I don't know where the Event__2 class gets the const specifier. Any advice?

Thank.

+1


source to share


1 answer


Probably the member function in which you initialize X is marked as const - something like that.

class Foo
{
   int *Bar;

public:

   void AssignAndDoStuff() const
   {
      Bar = new int; // Can't assign to a const object.
      // other code
   }
}

      

The solution here is either



  • Assign Bar in a separate non-const method,
  • change AssignAndDoStuff to be non-const, or
  • mark Bar like mutable

    .

Choose one of the above:

class Foo
{
   mutable int *Bar; // 3

public:
   void Assign() // 1
   {
       Bar = new int; 
   }   
   void DoStuff() const
   {
       // Other code
   }

   void AssignAndDoStuff() // 2
   {
      Bar = new int; 
      // other code
   }
}

      

+3


source







All Articles