A question about class members and constructors
I have the following class:
Code:
class Stuff
{
private:
int val;
public:
Stuff (int Val)
{
this.val = Val;
}
int GetVal()
{
return this.val;
}
void SetVal(int Val)
{
this.val = Val;
}
};
The bold line gives me the following error:
error C2228: left of '.val' must have class/struct/union
If I change it to
I get the following error:
error C2143: syntax error : missing ';' before '.'
What would be the correct way of creating a constructor that takes an int and assigns this int to val?
In general, what would be the correct way to address a variable of the class?
In C# I'd use this to refer to the current object, but I can't seem get that to work in C++.
Also, when I create a new Stuff object, what would be the correct syntax?
Wouldcreate a new, uninitialized object of the Stuff class with static memory
and woulddynamically allocate a new object the Stuff class, with an initialized value of 2, which is assigned to the variable val?
Thanks in advance.