9

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);
maximus
  • 4,201
  • 15
  • 64
  • 117
  • 2
    Note that in C++, `volatile` does not guarantee thread-safety. That's different from, for example, .NET, where it does. – sbi Jun 20 '10 at 07:49

4 Answers4

20

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
  • 33,081
  • 21
  • 109
  • 146
  • 2
    What about constructors and destructors and variables(properties) declared in class? – maximus Jun 20 '10 at 04:44
  • 2
    @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 Jun 20 '10 at 04:56
  • Thank you very much! Now everything is ok! – maximus Jun 20 '10 at 07:02
7

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

AviD
  • 302
  • 1
  • 6
4

In C++ grammar, "volatile" and "const" are called "CV modifiers". That means "volatile" works in exact the same way as "const" from syntactic point of view. You can replace all "volatile" with "const" then you can understand why your code compiles or not.

Windoze
  • 321
  • 2
  • 13
3

Yep. We can use. Please see the modified code. I hope it should work now.

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

    .....

EnterCriticalSection(&CriticalSection);
dataIn.push(element);
LeaveCriticalSection(&CriticalSection);
ketan
  • 19,129
  • 42
  • 60
  • 98
Kuppappa DH
  • 121
  • 1
  • 2