What are the get{} set{} accessor equivalents in C++?
Printable View
What are the get{} set{} accessor equivalents in C++?
You have to write your own, unless you're writing managed c++.
Code:protected:
int m;
public:
int GetM() const;
void SetM(int newM);
I don't understand the syntax of putting const after the function. Could you explain that and give an example of how the user would access and change the variable?
Consider:
When the const keyword appears at the end of the function declaration in a class, it means that calling that function will not modify any of the data in that class. Only members specified with the const keyword like this can be called if the object is marked as const:Code:class a
{
public:
int func() const;
int func2();
}:
Z.Code:int x(const a& blah)
{
blah.func(); // legal, func() is const
blah.func2() // illegal, is NOT const
return 0;
};
Is there really any point in doing things that way? The function itself controls what it does not any variables sent to it.
Well anyway, in VS .net, how does the designer know if you make your own control what variables are properties of the class?
The notion of properties does not exist in C++. Classes have member variables and member functions. A variable might be public, then you can simply access it. Or it might be private/protected, then you need access functions. This is similar to properties, but there is no built-in mechanism for properties.
There is one really good reason for this. Consider this code:
obj.member.member.member.member.member = 4;
How many function calls are there? In C++, you know it nearly exactly. Zero calls, except if the last member is of an object type that has overloaded the = operator, but you would probably know that.
In C#, who can say? How many of those members are just variables and how many are properties which imply a call to the getter?
Since performance is an important part of C++, it's good to know what happens.
About the const.
It does make sense. The const keyword is an important way to avoid bugs that come from accidental modification of data. Using const you can tell the compiler that this variable must not be modified. Using const the compiler can create temporary objects that would be dangerous or useless otherwise. Conside this code that passes a string to a function.
As you see, func3 (using const) is the perfect solution. But now we need to rely that the string really isn't modified. How to ensure that? The methods called might modify the internal variables anyway.Code:// The functions to be called.
void func1(string s);
void func2(string &s);
void func3(const string &s);
// Variable for passing.
string st("hello");
// Passing:
func1(st); // Valid. A copy of st is created and passed by value.
// Problem: st gets copied. A large object is passed by value.
func2(st); // Valid. A reference to st is passed.
// Problem: st can be modified. This might not be what you intend.
func3(st); // Valid. A constant reference to st is passed.
// This avoids both problems.
func1("hello"); // Valid. A temporary object is constructed and passed by value.
// Problem: passing by value again.
func2("hello"); // Invalid. You may not pass a reference to a temporary object.
// Problem: does not compile.
func3("hello"); // Valid. A temporary object is created and a constant reference is passed.
// Again avoids both problems.
The solution are const methods.
How do they work?
Let me take you further back :)
The first C++ compilers were actually preprocessors which translated the C++ code to C code. A class would be compiled like this:
Input
OutputCode:class MyClass
{
int myvar;
int othervar;
public:
MyClass() : myvar(3), othervar(3) {}
void dostuff(int param) {
myvar = param;
}
};
int main()
{
MyClass obj;
obj.dostuff(29);
return 0;
}
Such a compiler would translate a const method like this:Code:struct MyClass
{
int myvar;
int othervar;
};
_MyClass_constr_v(struct MyClass *this)
{
this->myvar = 3;
this->othervar = 3;
}
void _MyClass_dostuff_v_i(struct MyClass *this, int param)
{
this->myvar = param;
}
int main()
{
struct MyClass obj;
_MyClass_constr_v(&obj);
_MyClass_dostuff_v_i(&obj, 29);
return 0;
}
Code:class MyClass { ...
int getmy() const {
return myvar;
}
};
What does that mean? It means that if getmy attempted to change any value it would fail because the this pointer is constant. It means another thing:Code:int _MyClass_getmy_c_i_v(const struct MyClass *this)
{
return this->myvar;
}
Code:void foo(const MyClass &ref)
{
ref.dostuff(32);
}
Code:void foo_v_cr_MyClass(const struct MyClass *ref)
{
_MyClass_dostuff_v_i(ref, 32); // compile error, no implicit
// conversion from "const struct MyClass *" to "struct MyClass *".
}
Any questions left?
No. Thank you CornedBee.