class account
{
public:
int balance_;
};
class account
{
private:
int balance_;
};
class account
{
public:
int get() const
{
return balance_;
}
void set(int balance)
{
balance_ = balance;
}
private:
int balance_;
};
class account
{
public:
int get_val() const
{
return balance_;
}
void set_val(int balance)
{
if(balance < 0)
{
// ERROR!
}
balance_ = balance;
}
private:
int balance_;
};
template <typename pairT>
void func(pairT & mypair)
{
mypair.first = 320;
mypair.second = 240;
}
std::pair<int, int> mypair;
func(mypair);
class foo_pair
{
public:
int get_first() const
{
return mypair_.first;
}
int get_second() const
{
return mypair_.second;
}
void set_first((int val)
{
if(val < 1 || val > 100)
throw std::invalid_argument(
"value must be between 1 and 100"
);
mypair_.first = val;
}
void set_second((int val)
{
if(val < 1 || val > 100)
throw std::invalid_argument(
"value must be between 1 and 100"
);
mypair_.first = val;
}
private:
std::pair<int, int> mypair_;
};
foo_pair mypair;
func(mypair);
int x;
int y;
int z = x = y = 10;
int x;
int y;
int z = x.set(10) = y.set(10); // this makes no sense
foo x;
foo y;
x.set(10);
y.set(10);
int z = 10;
foo x;
foo y;
int z = x.set(y.set(10));
class foo_pair
{
public:
int first
{
get
{
return first_;
}
set
{
if(value < 1 || value > 100)
{
// ERROR!
}
first_ = value;
}
}
int second
{
get
{
return second_;
}
set
{
if(value < 1 || value > 100)
{
// ERROR!
}
second_ = value;
}
}
private:
int first_;
int second_;
};
#define property_(TYPE, OWNR, NAME, IMPL) \
private: \
class NAME ## __ \
{ \
friend class OWNR; \
public: \
typedef NAME ## __ this_type; \
typedef TYPE value_type; \
NAME ## __ () {} \
explicit NAME ## __ (value_type const & value) \
: NAME ## (value) {} \
IMPL \
private: \
value_type NAME ##; \
}; \
public: \
NAME ## __ NAME;
#define get_ \
operator value_type const & () const \
{ \
return get(); \
} \
value_type const & get() const
#define set_ \
this_type & operator = (value_type const & value) \
{ \
set(value); \
return *this; \
} \
void set(value_type const & value)
#define xprop_(NAME) \
NAME ## . ## NAME
#include <property.hpp>
class foo_pair
{
public:
foo_pair() :
first(9), second(5) // initilising properties (optional, if not used they will default construct)
{}
// Implement get, set and ++ operators
property_(
int, foo_pair, first, // int foo_pair::first
get_
{
return first;
}
set_
{
if(value < 1 || value > 100)
{
// ERROR!
}
first = value;
}
this_type & operator ++() { set(first +1); return *this; }
this_type operator ++(int)
{
this_type tmp(first);
set(first +1);
return tmp;
}
);
// Implement get, set and += operator
property_(
int, foo_pair, second, // int foo_pair::second
get_
{
return second;
}
set_
{
if(value < 1 || value > 100)
{
// ERROR!
}
second = value;
}
this_type operator += ( int rhs )
{
set(second + rhs);
return *this;
}
);
private:
void bypass() // for internal use by foo_pair only!
{
// accessing property members, circumventing set and get
xprop_(first) = 0;
xprop_(second) = 100;
}
};
template <typename pairT>
void func(pairT & mypair)
{
mypair.first = 320;
mypair.second = 240;
}
int main()
{
foo_pair mypair;
func(mypair); // Look, we can now call the generic function
int z = mypair.first = mypair.second; // Look, we can now perform expressions using variable and not function syntax
// Some additional operator manipulation
mypair.first++;
++mypair.first;
mypair.second+=10;
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (3)
Commented:
Author
Commented:Commented:
or like change functions with child class