Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't assign an object to a volatile object

Tags:

c++

I want to assign an object to volatile object in the same type, but failed to do so with compiler error. How to change the program to make it? Besides to make it to work, why i can't do it directly?

I used Visual Studio 2010 as compiler here.

class A
{
public:
};

int _tmain()
{
    A a;
    volatile A va;
    va = a;        // compiler error:C2678 here
    return 0;
}
like image 220
Thomson Avatar asked Jan 10 '11 06:01

Thomson


1 Answers

You need to define an assignment operator function for A with the volatile qualifier.

class A
{
    public:

    volatile A& operator = (const A& a) volatile
    {
      // assignment implementation
    }
};

If you don't define an assignment operator for a class, C++ will create a default assignment operator of A& operator = (const A&);. But it won't create a default assignment operator with the volatile qualifier, so you need to explicitly define it.

like image 116
Charles Salvia Avatar answered Sep 20 '22 19:09

Charles Salvia