Opinions on my get/set approach
I did a forum and google search on c++ getters and setters and found way too many hits and many diverse opinions.
I would ask your opinion on the approach I present here.
Early in my c++ studies I found I missed the built in getters and setters from my early VB days and more recently from PowerBASIC COM classes. I found a c++ template which I have used but it was lacking in it's implementation.
Recently I ran across another approach which I suplimented with the macros: PropGet,PropSet and PropGetSet.
Early on in my c++ education I adopted the method of prefixing all my private/protected variables with "m_"
This provides me with setters/getters with a minimum amount of coding.
Thank you,
James
Code:
#include <iostream>
#include <string>
typedef signed char SCHAR;
using namespace std;
//==============================================================================
#define PropGet(p,t) t p(){return m_##p;}
#define PropSet(p,t) void p(t p){m_##p=p;}
#define PropGetSet(p,t) PropGet(p,t)PropSet(p,t)
//==============================================================================
class PropTestOne
{
protected:
int m_Count;
long m_v1;
float m_f1;
string m_S1;
SCHAR m_c1;
public:
PropGetSet(Count, int);
PropGetSet(v1, long);
PropGetSet(f1, float);
PropGetSet(S1, string);
PropGetSet(c1, SCHAR);
};
class PropTestTwo
{
protected:
string m_first;
string m_mid;
string m_last;
string m_full;
public:
PropGetSet(first, string);
PropGetSet(mid, string);
PropGetSet(last, string);
PropGetSet(full, string);
};
int f1 (int n1, int n2)
{
return n1 + n2;
}
int main ()
{
PropTestOne test1;
PropTestTwo test2;
int i = {0};
int j = {0};
float f = {0.0};
SCHAR c = {0};
string xs;
//assign the value 5 to i
i = 5;
//assign test1 Count variable the value of i
test1.Count(i);
//Print the contents of test1's Count Variable
cout << test1.Count() << endl;
//assign the value 8 to i
i = 8;
//assign test1 Count variable the value of i again
test1.Count(i);
//assign j the value of it's Count Variable
j = test1.Count();
//did it work?
cout << "j = " << j << endl;
//Continue......
test1.Count(327);
cout << "test1.Count = " << test1.Count() << endl;
//use v1
test1.v1(134567);
j = test1.v1();
cout << " j = " << j << endl;
//assign one test1 var to another
test1.v1(test1.Count());
cout << "test1.v1 = " << test1.v1() << endl;
//do a bit of addition
j = test1.v1() + test1.Count();
cout << "j = " << j << endl;
//how about floats?
test1.f1(134.567);
f = test1.f1();
cout << "f = " << f << endl;
f = test1.f1() + 100.789321;
cout << "f = " << f << endl;
//will it work with a c++ std library string
test1.S1("James");
xs = test1.S1();
cout << " xs = " << xs << endl;
test1.c1(-88);
c = test1.c1();
cout << "c = " << (float)c << endl;
//Print "c = ";c
test2.first("James");
test2.mid("C.");
test2.last("Fuller");
xs = test2.first() + " " + test2.mid() + " " + test2.last();
test2.full(xs);
cout << "xs = " << xs << endl;
j = f1( test1.v1(), test1.Count());
cout << "pass props to function" << endl;
cout << "j = " << j << endl;
}