tags:

views:

92

answers:

2

Can the volatile be used for class objects? Like:

volatile Myclass className;

The problem is that it doesn't compile, everywhere when some method is invoked, the error says: error C2662: 'function' : cannot convert 'this' pointer from 'volatile MyClass' to 'MyCLass &'

What is the problem here and how to solve it?

EDIT:

class Queue {
            private:
                struct Data *data;
                int amount;
                int size;
            public:
                Queue ();
                ~Queue ();
                bool volatile push(struct Data element);
                bool volatile pop(struct Data *element);
                void volatile cleanUp();
            };
    .....
    volatile Queue dataIn;

        .....

    EnterCriticalSection(&CriticalSection);
    dataIn.push(element);
    LeaveCriticalSection(&CriticalSection);
+3  A: 

Yes, you can, but then you can only call member functions that are declared volatile (just like the const keyword). For example:

 struct foo {
    void a() volatile;
    void b();
 };

 volatile foo f;
 f.a(); // ok
 f.b(); // not ok

Edit based on your code:

bool volatile push(struct Data element);

declares a non-volatile member function that returns a bool volatile (= volatile bool). You want

bool push(struct Data element) volatile;
Jesse Beder
What about constructors and destructors and variables(properties) declared in class?
maximus
I did as you said, but anyway the same error messages..
maximus
@maximus, constructors and destructors can't be overloaded with `volatile` (or `const`), and member variables inherit these properties from the class instance. Regarding your error messages, please post the exact code you're using.
Jesse Beder
I added code, please refer to that
maximus
Thank you very much! Now everything is ok!
maximus
+2  A: 

I think he meant to say

            bool push(struct Data element) volatile;

instead of

            bool volatile push(struct Data element);

Also have a look here http://www.devx.com/tips/Tip/13671

Game
Thank you very much! It works!
maximus