-2

It occurs to me that, I should reword my question. Is there any difference between these two implementation when the compiler does its optimization?

Sorry about the confusion. I do understand the importance of encapsulation and that is not what i am asking about.

class A
{
public:
 int get_data{return m_data;}

private:
 int m_data;
}

class B
{
public:
 int get_data{return m_data;}

private:
 volatile int m_data;
}
Frank Liu
  • 1,466
  • 3
  • 23
  • 36
  • 2
    I think in this case, if this were in a header, the `inline` would be implied. – Karthik T May 07 '14 at 07:29
  • 1
    Define same, there are a lot of reasons to do a getter vs exposing the variable itself – Karthik T May 07 '14 at 07:30
  • Am not too familiar with `volatile` is there anything about it that prevents outsiders changing the value? – Karthik T May 07 '14 at 07:31
  • if use the getter, do i have to worry about m_data got cached in the process? – Frank Liu May 07 '14 at 07:32
  • 3
    Rather than programming by random guessing, it might be more productive to learn the language systematically, and e.g. understand what `volatile` is for. – Kerrek SB May 07 '14 at 07:32
  • 2
    What do you understand by `volatile`? What makes you think that having a "getter" would imply `volatile`? – CB Bailey May 07 '14 at 07:32
  • @FrankLiu cached? unless the variable was `const` i wouldnt expect that. And even in the case of `const` – Karthik T May 07 '14 at 07:33
  • encapsulation apart. does compiler optimize the getter function to cache the returned value? – Frank Liu May 07 '14 at 07:33
  • It can. And probably does. – Alex Shtoff May 07 '14 at 07:34
  • my understanding of volatile is that the processor will read/write directly to the memory. Without volatile, the compiler may optimize the code that the value might get cached in some internal register. Does use a getter prevent the caching without declare m_data volatile. – Frank Liu May 07 '14 at 07:38
  • @KerrekSB Rather than sound condescending, it might be more productive to give some constructive suggestions. like you know everything. – Frank Liu May 07 '14 at 07:41
  • @FrankLiu _Without volatile, the compiler may optimize the code that the value might get cached in some internal register_ - And how would introducing a getter method prevent the compiler from doing so? As pointed out by the answers, the two concepts (`volatile` and getter functions) are completely unrelated, but I'm still curious as to what you think the link between the two would be. – ComicSansMS May 07 '14 at 07:44
  • @FrankLiu: I gave a constructive suggestion: Learn systematically what the rules are. I don't pretend to, or in fact actually know what they are, but if I wanted to use `volatile`, I'd look up what it means and how it behaves, rather than guess that sprinkling it into some random code would have some kind of effect. (There is in fact a non-trivial, subtle place for `volatile` in the C++ memory model, but it's unlikely to be useful.) – Kerrek SB May 07 '14 at 19:05

4 Answers4

3

No. A function call does not imply that access to this variable cannot be re-ordered or optimized out. It can be optimized and re-ordered, and most compilers will probably perform such optimizations.

Alex Shtoff
  • 2,520
  • 1
  • 25
  • 53
1

I was wondering if accessing a data member via a getter is the same as making it public and declare it volatile ?

No. BTW correct usage of volatile in C and C++ is so rare that I wonder if you are mistaken and think it has something to do with threads, it hasn't.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143
0

Volatile is mostly used in code which interacts with memory mapped IO etc. It prevents the compiler from most optimizations and therefore CAN lead to the desired behaviour in multi-threaded code, but its intent is to decorate memory which the compiler knows nothing about.

Here is an article from Herb Sutter about this topic.

DAXaholic
  • 33,312
  • 6
  • 76
  • 74
0

The reasons for getters is the old story about encapsulation. Why use getters and setters?

No matter what keyword you use, the encapsulation rule will be broken if you are not using a getter

And as everyone else has pointed out, volatile does not do what you think it does.

Community
  • 1
  • 1
Leon
  • 12,013
  • 5
  • 36
  • 59