C++ Custom Member to Member "Pointer" / Access - c++

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?

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;

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);
}

Related

How to invoke struct member functions based on return value of a bool type member function?

I want to do some conditional checks within a bool member function of a struct. How will my struct object struct1 know the bool member function has returned true, so that integers a & b can be used within the calc member function?
int main() {
vector<Point> pt;
pt.push_back(Point{ 1.5, 4.2 });
pt.push_back(Point{ 2.4, 3.1 });
doSth struct1;
bool tempbool = struct1.memfuncbool(pt); //error starts here!
if (tempbool) {int answer = struct1.calc(1);} //??
std::cout << answer;
return 0;
}
struct Point {
double _x;
double _y;
};
struct doSth {
int a, b; //data members
int calc(const int k) {
return (a + b)*k;
}
bool memfuncbool(const vector<Point> &pts) {
//does stuff...
a = var1; //var1 = 1
b = var2; //var2 = 2
return true;
}
}
There are two approaches: encapsulation for caller safety and pure code discipline. In the later you yourself make sure that the code you write is always aware of the latest result of memfuncbool and when and where a and b are set.
In the first you can add a flag in your struct that you set once memfuncbool is called and check in calc (and handle it appropriately.) In that case you should also make sure that the flag is cleared when initialising your struct - either via constructor or again code discipline (like always zero your structs).
An information hiding approach (C++) in the first sense would look like this:
class DoSth {
int a, b;
bool valid;
public:
DoSth() : valid(false) { }
bool isValid() const { return valid; }
/// returns calc if valid, otherwise 0
int calc(int k) const {
return isValid() ? (a + b) * k : 0;
}
void setSth(...) {
a = ...
b = ...
valid = true;
// instead of returning here the caller can check isValid() anytime
}
};
Your code has many issues that could easily be solved if you didn't try to do more than you know.
1 You're defining both Point and doSth structures AFTER the main function. So the main function has no way to know what your structures do. Normally you would use a header file to contain the declaration and the cpp file to contain the implementation, but since you're doing a small program you can just move the definitions of your structures to be above the main function. Alternatively you can declare your structures above main and implement them below, like this.
// Definition of struct Point
struct Point {
double _x;
double _y;
};
// Definition of struct doSth
struct doSth {
int a, b; //data members
// **Declaration** of doSth methods
int calc(const int k);
bool memfuncbool(const std::vector<Point> &pts);
};
int main() {
...
}
// Definition of calc method
int doSth::calc(const int k) {
...
}
// Definition of memfuncbool method
bool doSth::memfuncbool(const std::vector<Point> &pts) {
...
}
2 In the main function you're using a variable called answer in a scope that doesn't know such variable.
if (tempbool) {
int answer = struct1.calc(1);
}
std::cout << answer; // ERROR: answer is not a known variable
See, you're declaring a variable inside an if condition but using it outside. You have to declare the variable outside as well if you want to use it.
int answer = 0;
if ( tempbool ) {
answer = struct1.calc(1);
}
std::cout << answer << std::endl; // OK
OR
if ( tempbool ) {
int answer = strut1.calc(1);
std::cout << answer << std::endl;
}
else {
std::cout << "Invalid result!" << std::endl;
}
This is a fix to compile what you have done so far. But this is not a solution to your code design question.
3 Code Design
Although I suggested quick fixes to your code your actual problem has to do with how you design your classes and structure your code. I've showed you what you did wrong in your code, but your problem can be solved using a better structured approach.
While writing my answer Beyeler already answered this part for you, so check his answer.
EDIT
In your code you are probably doing
using namespace std;
But you've written both vector< Point > and std::cout. You should not use the using line, to begin with, to avoid name collisions and to help YOU know where this vector is coming from.
However, if you insist on using this line so you don't have to write std:: (and it's OK if you know what you're doing), don't go typing std:: on one thing and then omit it in another. Be consistent, either use it or don't.
I found three errors with your code:
"do" is a c++ keyword. You can't use it to name structs.
The parameter of the "memfuncbool" is missing its type. const& is not a type.
Missing semicolon after struct definition.
Also i supposed that var1, var2 and arg are well defined. If they are not even that is a error.
After correcting these errors maybe you can do something like-
if(tempbool) { /*statements*/ };

Is it possible to access a variable in a sketch from a class (Arduino)?

Say I want to get a value of a variable in a sketch from a class I wrote like
sketch
int device;
void setUp() {
device = 1;
}
And I have a class
SomeClass.cpp
void Device::checkTimedEvent() {
someDevice = device; //variable from sketch
}
I know it's possible to access members from another class where I can include the class and use the :: scope operator but not sure how the sketch relates to classes.
thanks
It appears that the usual C/C++ "extern" syntax works in Arduino as if the sketch file were a .cpp file:
Sketch:
int device = 123;
SomeClass.cpp:
extern int device;
void SomeClass::checkTimedEvent() {
someDevice = device; // variable from sketch
// will display "123" (or current value of device)
// if serial output has been set up:
Serial.println("device: " + String(device));
}
You may need to worry about startup and initialization order, depending on the complexity of your project.
class Test
{
public:
int N = 0;
};
Test t;
int someNumber = t.N;
The best way to do this is to pass the value into the function as a parameter. Here's a simple example:
Class:
class Test
{
public:
void doSomething(int v);
private:
int myValue;
};
void Test::doSomething(int v)
{
myValue = v;
}
Sketch:
Test t;
int someNumber;
void setup()
{
someNumber = 27;
t.doSomething(someNumber);
}
The setup() function here passes global variable someNumber into the class's member function. Inside the member function, it stores its own copy of the number.
It's important to note that it has a completely independent copy of the number. If the global variable changes, you'd need to pass it in again.
As much as Bloomfiled's answer is correct using the the more accepted practice of employing Getter and Setter functions. Below demonstrates this along with making the attribute public and directly accessing it.
class Test
{
public:
void SetMyValue(int v);
int GetPublicValue();
int GetPrivateValue();
int myPublicValue;
private:
int myPrivateValue;
};
void Test::SetMyValue(int v)
{
myPublicValue = v;
myPrivateValue = v;
}
int Test::GetPublicValue()
{
return myPublicValue;
}
int Test::GetPrivateValue()
{
return myPrivateValue;
}
Test t;
int someNumber;
void setup()
{
someNumber = 27;
t.SetMyValue(someNumber); // set both private and public via Setter Function
t.myPublicValue = someNumber; // set public attribute directly.
someNumber = t.GetPublicValue(); // read via Getter
someNumber = t.GetPrivateValue();
someNumber = t.myPublicValue; // read attribute directly
}
void loop() {
// put your main code here, to run repeatedly:
}

Increment value from void pointer

I'm trying to modify some variables [not necessary from the same class/struct] from keyboard's shortcuts, something like that:
A foo struct containing variables:
struct Foo {
int a;
float b;
};
struct Foo2 {
int c;
};
And a main like:
int main() {
Foo f;
Foo2 f2
void* p = &(f.a); //it could be &(f2.c)
if ('A' key activated) {
*p += 1;
}
}
Currently, I'm stucked at this point:
error: invalid operands to binary expression ('void' and 'int')
The only way to make it work is to change:
*p += 1;
By:
*(int*)p += 1;
Which is not a good solution, because I should not know the type pointed by p. Is there a way to do that?
Converting the pointer to void* lost the type information and the compiler will not know how to increment. Why don't you make a pointer to Foo instead?
int main() {
Foo f;
Foo* p = &f;
if ('A' key activated) {
p->a += 1;
}
}
Also keep in mind that incrementing a float is not a good idea!
For the quesion in the comment of this answer:
struct FooBar
{
int *a;
float *b;
};
int main() {
Foo f;
Bar b;
FooBar fb{&f.a, &b.b};
if ('A' key activated) {
*(fb.a) += 1;
}
}
Note that this solution is rather C-style. Look at lethal-guitar's answer for a more C++-style solution.
Edit: At first I didn't realize that you want to have different types per entry. Based on the task of handling keyboard shortcuts, you could use a polymorphic class, and put instances of it into a std::map:
class KeyHandler {
public:
virtual void onKeyStroke() = 0;
};
class MyHandler : public KeyHandler {
public:
MyHandler(int& value) : myValue(value) {}
virtual void onKeyStroke() {
myValue_ += 1;
}
private:
int& myValue_; // Other subclasses could have other data
};
// Now place instances of different Handlers into a std::map
typedef std::shared_ptr<KeyHandler> PKeyHandler;
std::map<char, PKeyHandler> bindings;
bindings['A'] = PKeyHandler(new IncrementIntHandler(&someInt));
bindings['B'] = PKeyHandler(new IncrementFloatHandler(&someFloat));
// The actual input handler then just invokes
// the correct handler for a key stroke.
bindings[keyCode]->onKeyStroke();
That way, you can define a handler class for every action you want to support, and implement the corresponding logic into these classes. You could make the base class' implementation just do nothing to handle non-mapped keys, etc.
Sure, use an int pointer instead:
int * p = &f.a;
if ( /* condition */ ) { ++*p; }

c++ redefine variable as constant

I have a struct:
struct s
{
UINT_PTR B_ID;
};
s d;
d.B_ID=0x1;
That works fine, but I want d.B_ID to be constant. I tried to use (const) but it didn't work. So after I put a value to d.B_ID, then I want make it a constant.
Any ideas?
EDIT
ok i don't want the whole struct a constant.
when i set timer and use the b.B_ID as an idea for the timer.
in the
switch(wparam)
{
case b.B_ID // error: B_ID must be constant
....
break;
}
so that is why i need it to be a constant
Variable modifiers are fixed at compile time for each variable. You may have to explain the context of what you are trying to do, but perhaps this will suit your needs?
struct s
{
int* const B_ID;
};
int main (void) {
int n = 5;
s d = {&n};
int* value = d.B_ID; // ok
// d.B_ID = &n; // error
return 0;
}
Since you are using C++ I would recommend:
class s {
public:
int* const B_ID;
s (int* id) :
B_ID (id) {
}
};
void main (void) {
int n = 5;
s my_s_variable = s(&n);
int* value = my_s_variable.B_ID; // ok
//my_s_variable.B_ID = &n; // error
return 0;
}
Ramiz Toma: well i need way to do it using the s.B_ID=something
In C/C++ type modifiers (like const) are declared at run time for a given type and cannot be changed at run time. This means that if a variable is declared const it can never be assigned to using the assignment operator. It will only be assigned a value when it is constructed.
This is not a problem however because you can always get around this by proper design.
If you say you need to use assignment, I assume that this is because you create the struct before you know what the value of the variable will be. If this is the case then you simply need to move the struct declaration till after you know the value.
For example
s d; //variable declaration
//calculate B_ID
//...
int* n = 5;
//...
d.B_ID = &n;
This will not work, because if you want b.D_ID to be 'un assignable' it will always be so. You will need to refactor your code similarly to:
//calculate B_ID
//...
int* n = 5;
//...
s d (&n);
//good
struct s
{
s() : B_ID(0){}
UINT_PTR const B_ID;
};
int main(){
s d;
d.B_ID=0x1; // error
}
EDIT: Sorry, here is the updated code snippet in C++
struct s
{
s(UINT_PTR const &val) : B_ID(val){}
UINT_PTR const B_ID;
};
int main(){
s d(1);
d.B_ID=0x1; // error
}
In C++ language the case label must be built from an Integral Constant Expression (ICE). ICE is what the compiler implies under the term "constant" in your error message. A non-static member of a class cannot be used in an ICE. It is not possible to do literally what you are trying to do. I.e. it is not possible to use a struct member in a case label.
Forget about switch/case in this context. Use ordinary if branching instead of switch statement.
You can't do that - ie. it is not possible to selectively make a single member of a struct const. One option is to 'constify' the entire struct:
s d;
d.B_ID=0x1;
const s cs = s; // when using this B_ID won't be modifiable - but nor would any other members
Or you could set it at construction:
struct s
{
s(UINT_PTR const p): B_ID(p) {}
UINT_PTR const B_ID;
};
s d(0xabcdef);
Another way would be a getter and a one time setter
class s
{
private:
bool m_initialized;
UINT_PTR m_value;
public:
s() : m_initialized(false), m_value(NULL) {}
s(UINT_PTR value) : m_initialized(true), m_value(value) {}
//no need for copy / assignment operators - the default works
inline UINT_PTR GetValue() const { return m_value; } //getter
bool SetValue(UINT_PTR value) //works only one time
{
if (m_initialized)
{
m_value = value;
m_initialized=true;
return true;
}
else
{
return false;
}
}
inline bool IsInitialized() const { return m_initialized; }
};

Does C++ have "with" keyword like Pascal?

with keyword in Pascal can be use to quick access the field of a record.
Anybody knows if C++ has anything similar to that?
Ex:
I have a pointer with many fields and i don't want to type like this:
if (pointer->field1) && (pointer->field2) && ... (pointer->fieldn)
what I really want is something like this in C++:
with (pointer)
{
if (field1) && (field2) && .......(fieldn)
}
Probably the closest you can get is this: (this is just an academic exercise. Of course, you can't use any local variables in the body of these artificial with blocks!)
struct Bar {
int field;
};
void foo( Bar &b ) {
struct withbar : Bar { void operator()() {
cerr << field << endl;
}}; static_cast<withbar&>(b)();
}
Or, a bit more demonically,
#define WITH(T) do { struct WITH : T { void operator()() {
#define ENDWITH(X) }}; static_cast<WITH&>((X))(); } while(0)
struct Bar {
int field;
};
void foo( Bar &b ) {
if ( 1+1 == 2 )
WITH( Bar )
cerr << field << endl;
ENDWITH( b );
}
or in C++0x
#define WITH(X) do { auto P = &X; \
struct WITH : typename decay< decltype(X) >::type { void operator()() {
#define ENDWITH }}; static_cast<WITH&>((*P))(); } while(0)
WITH( b )
cerr << field << endl;
ENDWITH;
no there is no such keyword.
I like to use:
#define BEGIN_WITH(x) { \
auto &_ = x;
#define END_WITH() }
Example:
BEGIN_WITH(MyStructABC)
_.a = 1;
_.b = 2;
_.c = 3;
END_WITH()
In C++, you can put code in a method of the class being reference by pointer. There you can directly reference the members without using the pointer. Make it inline and you pretty much get what you want.
Even though I program mostly in Delphi which has a with keyword (since Delphi is a Pascal derivative), I don't use with. As others have said: it saves a bit on typing, but reading is made harder.
In a case like the code below it might be tempting to use with:
cxGrid.DBTableView.ViewData.Records.FieldByName('foo').Value = 1;
cxGrid.DBTableView.ViewData.Records.FieldByName('bar').Value = 2;
cxGrid.DBTableView.ViewData.Records.FieldByName('baz').Value = 3;
Using with this looks like this
with cxGrid.DBTableView.ViewData.Records do
begin
FieldByName('foo').Value = 1;
FieldByName('bar').Value = 2;
FieldByName('baz').Value = 3;
end;
I prefer to use a different technique by introducing an extra variable pointing to the same thing with would be pointing to. Like this:
var lRecords: TDataSet;
lRecords := cxGrid.DBTableView.ViewData.Records;
lRecords.FieldByName('foo').Value = 1;
lRecords.FieldByName('bar').Value = 2;
lRecords.FieldByName('baz').Value = 3;
This way there is no ambiguity, you save a bit on typing and the intent of the code is clearer than using with
No, C++ does not have any such keyword.
The closest you can get is method chaining:
myObj->setX(x)
->setY(y)
->setZ(z)
for setting multiple fields and using for namespaces.
C++ does not have a feature like that. And many consider "WITH" in Pascal to be a problem because it can make the code ambiguous and hard to read, for example it hard to know if field1 is a member of pointer or a local variable or something else. Pascal also allows multiple with-variables such as "With Var1,Var2" which makes it even harder.
with (OBJECT) {CODE}
There is no such thing in C++.
You can put CODE as is into a method of OBJECT, but it is not always desirable.
With C++11 you can get quite close by creating alias with short name for OBJECT.
For example code given in question it will look like so:
{
auto &_ = *pointer;
if (_.field1 && ... && _.fieldn) {...}
}
(The surrounding curly braces are used to limit visibility of alias _ )
If you use some field very often you can alias it directly:
auto &field = pointer->field;
// Even shorter alias:
auto &_ = pointer->busy_field;
No, there is no with keyword in C/C++.
But you can add it with some preprocessor code:
/* Copyright (C) 2018 Piotr Henryk Dabrowski, Creative Commons CC-BY 3.0 */
#define __M2(zero, a1, a2, macro, ...) macro
#define __with2(object, as) \
for (typeof(object) &as = (object), *__i = 0; __i < (void*)1; ++__i)
#define __with1(object) __with2(object, it)
#define with(...) \
__M2(0, ##__VA_ARGS__, __with2(__VA_ARGS__), __with1(__VA_ARGS__))
Usage:
with (someVeryLongObjectNameOrGetterResultOrWhatever) {
if (it)
it->...
...
}
with (someVeryLongObjectNameOrGetterResultOrWhatever, myObject) {
if (myObject)
myObject->...
...
}
Simplified unoverloaded definitions (choose one):
unnamed (Kotlin style it):
#define with(object) \
for (typeof(object) &it = (object), *__i = 0; __i < (void*)1; ++__i)
named:
#define with(object, as) \
for (typeof(object) &as = (object), *__i = 0; __i < (void*)1; ++__i)
Of course the for loop always has only a single pass and will be optimized out by the compiler.
First I've heard that anybody doesn't like 'with'. The rules are perfectly straightforward, no different from what happens inside a class in C++ or Java. And don't overlook that it can trigger a significant compiler optimization.
The following approach relies on Boost. If your compiler supports C++0x's auto then you can use that and get rid of the Boost dependence.
Disclaimer: please don't do this in any code that must be maintained or read by someone else (or even by yourself in a few months):
#define WITH(src_var) \
if(int cnt_ = 1) \
for(BOOST_AUTO(const & _, src_var); cnt_; --cnt_)
int main()
{
std::string str = "foo";
// Multiple statement block
WITH(str)
{
int i = _.length();
std::cout << i << "\n";
}
// Single statement block
WITH(str)
std::cout << _ << "\n";
// Nesting
WITH(str)
{
std::string another("bar");
WITH(another)
assert(_ == "bar");
}
}
Having written numerous parsers, this seems like a dead simple list look up for the named object, either static or dynamic. Further, I have never seen a situation where the compiler did not correctly identify the missing object and type, so all those lame excuses for not allowing a WITH ...ENDWITH construction would seem to be a lot of hooey. For the rest of us prone to long object names one workaround is to create simple defines. Couldn't resist, suppose I have:
#include<something>
typedef int headache;
class grits{
public:
void corn(void);
void cattle(void);
void hay(void);}; //insert function defs here
void grits::grits(void)(printf("Welcome to Farm-o-mania 2012\n");};
#define m mylittlepiggy_from_under_the_backporch.
headache main(){
grits mylittlepiggy_from_under_the_backporch;
m corn(); //works in GCC
m cattle();
m hay();
return headache;
#include <iostream>
using namespace std;
template <typename T>
struct with_iter {
with_iter( T &val ) : p(&val) {}
inline T* begin() { return p; }
inline T* end() { return p+1; }
T *p;
};
#define with( N, I ) for( auto &N : with_iter<decltype(I)>(I) )
int main() {
with( out , cout ) {
out << "Hello world!" << endl;
}
return 0;
}
Nuf said ...
I can see one instance where 'with' is actually useful.
In methods for recursive data structures, you often have the case:
void A::method()
{
for (A* node = this; node; node = node->next) {
abc(node->value1);
def(value2); // -- oops should have been node->value2
xyz(node->value3);
}
}
errors caused by typos like this are very hard to find.
With 'with' you could write
void A::method()
{
for (A* node = this; node; node = node->next) with (node) {
abc(value1);
def(value2);
xyz(value3);
}
}
This probably doesn't outweight all the other negatives mentioned for 'with', but just as an interesting info...
Maybe you can:
auto p = *pointer;
if (p.field1) && (p.field2) && ... (p.fieldn)
Or create a small program that will understand with statements in C++ and translate them to some form of a valid C++.
I too came from the Pascal world..... .....and I also LOVE Python's use of with (basically having an automatic try/finally):
with open(filename, "r") as file:
for line in file:
if line.startswith("something"):
do_more()
That acts like a smart ptr object. It does not go into the block if the open failed; and when leaving the block, the file if closed.
Here is a sample very close to Pascal while also supporting Python's usage (assuming you have a smart object with destructor cleanup); You need newer C++ standard compilers for it to work.
// Old way
cxGrid_s cxGrid{};
cxGrid.DBTableView.ViewData.Records.FieldByName.value["foo"] = 1;
cxGrid.DBTableView.ViewData.Records.FieldByName.value["bar"] = 2;
cxGrid.DBTableView.ViewData.Records.FieldByName.value["baz"] = 3;
// New Way - FieldByName will now be directly accessible.
// the `;true` is only needed if the call does not return bool or pointer type
if (auto FieldByName = cxGrid.DBTableView.ViewData.Records.FieldByName; true)
{
FieldByName.fn1 = 0;
FieldByName.fn2 = 3;
FieldByName.value["foo"] = 1;
FieldByName.value["bar"] = 2;
FieldByName.value["baz"] = 3;
}
And if you want even closer:
#define with if
with (auto FieldByName = cxGrid.DBTableView.ViewData.Records.FieldByName; true)
// Similar to the Python example
with (smartFile sf("c:\\file.txt"); sf)
{
fwrite("...", 1, 3, *sf);
}
// Usage with a smart pointer
with (std::unique_ptr<class_name> p = std::make_unique<class_name>())
{
p->DoSomethingAmazing();
// p will be released and cleaned up upon exiting the scope
}
The (quick and dirty) supporting code for this example:
#include <map>
#include <string>
struct cxGrid_s {
int g1, g2;
struct DBTableView_s {
int tv1, tv2;
struct ViewData_s {
int vd1, vd2;
struct Records_s {
int r1, r2;
struct FieldByName_s{
int fn1, fn2;
std::map<std::string, int> value;
} FieldByName;
} Records;
} ViewData;
} DBTableView;
};
class smartFile
{
public:
FILE* f{nullptr};
smartFile() = delete;
smartFile(std::string fn) { f = fopen(fn.c_str(), "w"); }
~smartFile() { if (f) fclose(f); f = nullptr; }
FILE* operator*() { return f; }
FILE& operator->() { return *f; }
operator bool() const { return f != nullptr; }
};
I was lamenting to PotatoSwatter (currently accepted answer) that I could not access variables declared in the enclosing scope with that solution.
I tried to post this in a comment response to PotatoSwatter, but it's better as a whole post. It's all a bit over the top, but the syntax sugar is pretty nice!
#define WITH_SIG float x, float y, float z
#define WITH_ARG x, y, z
#define WITH(T,s) do { struct WITH : T { void operator()(s) {
#define ENDWITH(X,s) }}; static_cast<WITH&>((X))(s); } while(0)
class MyClass {
Vector memberVector;
static void myFunction(MyClass* self, WITH_SIG) {
WITH(MyClass, WITH_SIG)
memberVector = Vector(x,y,z);
ENDWITH(*self, WITH_ARG);
}
}
A simple way to do this is as follows
class MyClass
{
int& m_x;
public MyClass(int& x)
{
m_x = x;
m_x++;
}
~MyClass()
{
m_x--;
}
}
int main():
{
x = 0;
{
MyClass(x) // x == 1 whilst in this scope
}
}
I've been writing python all day long and just scrapped this down before anyone takes me to the cleaners. In a larger program this is an example of how to keep a reliable count for something.