0

I discovered a neat feature years ago while i was searching something on google. It enabled the usage of a sort of "function" to control access to a member variable but i can't seem to find it anymore. (I am also not sure if this was a c++ feature or only specific to the msvc compiler because it was highlighted red in visual studio as if it was label or something)

The theory behind it was something similar to this:

class A
{
public:
.test(int value)
{
  priv = value;
}
private:
int priv = 0;
};

...
A a;
a.test = 14; // Sets priv to 14 ! note no () needed after test´

Does anybody know what it is / was?

L. Terrat
  • 47
  • 1
  • 9
  • 2
    This is not legal C++ syntax. I think C# might have something like it with their `get` and `set` functionality. – NathanOliver Mar 12 '20 at 15:33
  • I know that it doesn't look like c++ syntax and i know what you are talking about but it still worked in the past but sadly i cant find it anymore. – L. Terrat Mar 12 '20 at 15:34
  • 1
    Are you perhaps looking for [pointers to members](https://stackoverflow.com/questions/670734/pointer-to-class-data-member)? – Brian61354270 Mar 12 '20 at 15:35
  • Do you mean the `friend` keyword? – Object object Mar 12 '20 at 15:35
  • 1
    You can make `a.test` an instance of a class with `operator=` for assignment and `operator int` for reading the value. But this is probably not a good idea. – interjay Mar 12 '20 at 15:36
  • @Brian Yes i was looking for that topic when i originally found it but it was like the way i described it in my question. – L. Terrat Mar 12 '20 at 15:39
  • @TheGoldKnight23 Also not the friend keyword it was defined inside the class itself. And also not the workaround described by interjay. – L. Terrat Mar 12 '20 at 15:40
  • Ok then my answer is probably not what your looking for but ill leave it up as I think this is the only way of doing this within C++ – Object object Mar 12 '20 at 15:41
  • yes, the feature exists, it's well known and used. Like so: `public int Test { set { priv = value } }` and use like so: `a.test = 24`. Oh, and by the way it's in C# – bolov Mar 12 '20 at 15:56
  • Possibly a C++/CLI or C++/CX feature. Neither of which is C++. – Eljay Mar 12 '20 at 16:11
  • @bolov No it was not C# like i already said before, I posted the answer down below. – L. Terrat Mar 12 '20 at 16:37

2 Answers2

1

Thank you everyone for responding but no, it was not C# like some people desperately tried to tell me.

Microsoft docs - property (C++)

For those interested how it worked:

struct S
{
    int i;
    void putprop(int j) {
        i = j;
    }

    int getprop() {
        return i;
    }

    __declspec(property(get = getprop, put = putprop)) int the_prop;
};

 S s;
    s.the_prop = 5;
    int test = s.the_prop;
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
L. Terrat
  • 47
  • 1
  • 9
0

Designated initializer

If I had to speculate, you've most probably seen the C99 designated initializer

It looks like this:

MY_TYPE a = { .flag = true, .value = 123, .stuff = 0.456 };

This is a C only thing and doesn't exist in C++. There has been a proposal for C++20 that has been accepted, to include limited support for them: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0329r4.pdf

C++/CLI Property

The other thing that comes to mind is properties, part of managed C++.

You'd use them like that (source: https://learn.microsoft.com/en-us/cpp/extensions/property-cpp-component-extensions?view=vs-2019)

public ref class C {
   int MyInt;
public:

   // property data member
   property String ^ Simple_Property;

   // property block
   property int Property_Block {

      int get();

      void set(int value) {
         MyInt = value;
      }
   }
};

int C::Property_Block::get() {
   return MyInt;
}

int main() {
   C ^ MyC = gcnew C();
   MyC->Simple_Property = "test";
   Console::WriteLine(MyC->Simple_Property);

   MyC->Property_Block = 21;
   Console::WriteLine(MyC->Property_Block);
}
Community
  • 1
  • 1
divinas
  • 1,787
  • 12
  • 12