i'm build a completed new code for properties:

#include <iostream>
#include <functional>

using namespace std;

template <typename T>
class property
{
private:
    T PropertyValue;
    //i think these is ok, but something seems not
    std::function<T(void)> getf;
    std::function<void(T)> setf;
public:
    property()
    {
        getf=NULL;
        setf=NULL;
    };

    property(T value)
    {
        PropertyValue=value;
    };

    property(std::function<void(void)> GetFunction=NULL,std::function<T(void)> SetFunction=NULL)// what isn't right here?
    {
        setf=SetFunction;
        getf=GetFunction;
    }

    property& operator=(T value)
    {
        if(getf==NULL || setf==NULL)
            PropertyValue=value;
        else
            setf(value);
        return *this;
    }

    T& operator=(property value)
    {
        if(getf==NULL || setf==NULL) 
            return PropertyValue;
        else
            return getf();
    }

    friend ostream& operator<<(ostream& os, const property& dt)
    {
        if(getf==NULL && setf==NULL) //error these don't make sence to me :(
            os << dt.PropertyValue;
        else if (getf!=NULL)
            os << getf();
        else
            return 0;
        return os;
    }

    friend istream& operator>>(istream &input,property &dt)
    {
        if(getf==NULL && setf==NULL) //error these don't make sence to me :(
            input << dt.PropertyValue;
        else if (setf!=NULL)
            input << setf();
        else
            return 0;
        return input;
    }
};

class test
{
private:
    int age;
    int getage()
    {
        return age;
    }
    void setage(int value)
    {
        age=value;
    }
public:

    test()
    {
        //nothing
    }
    property<string> Name;
    property<int> Age(&getage,&setage); //error

};

test a;

int main()
{
    a.Name="joaquim "  "Miguel";
    a.Age=10+15;
    cout << a.Age << endl;
    cout << "what is your name?\n";
    cin >> a.Age;
    cout << "your name is: " << a.Age << endl;
    return 0;
}

but i get 23 errors, if is need, i can give all the info.
sorry, but i'm not getting help :(
please someone tell me something

Recommended Answers

All 26 Replies

Most of your errors involves trying to use member variables but not stating which object they're from. For example:

friend ostream& operator<<(ostream& os, const property& dt)
    {
        if(getf==NULL && setf==NULL) //error these don't make sence to me :(
            os << dt.PropertyValue;
        else if (getf!=NULL)
            os << getf();
        else
            return 0;
        return os;
    }

should be

friend ostream& operator<<(ostream& os, const property& dt)
    {
        if(dt.getf==NULL && dt.setf==NULL)
            os << dt.PropertyValue;
        else if (dt.getf!=NULL)
            os << dt.getf();
        else
            return 0;
        return os;
    }

although I've no idea what that return 0 is doing there.

I suspect this:
property(std::function<void(void)> GetFunction=NULL,std::function<T(void)> SetFunction=NULL)
would be better as this:
property(std::function<void(void)> GetFunction=nullptr,std::function<T(void)> SetFunction=nullptr)
and this being C++11, nullptr is preferred to NULL.

commented: thanks for all +2

thanks for that information... thanks
now i have more errors:
on test class constructor i have these error:

"call of overloaded 'property()' is ambiguous"
these don't make sence to me.

and

property<int> Age(&getage,&setage);

give 2 same errors:
"expected identifier before '&' token"
i have more errors, but let see if we can fix these problems

Also, since the function returns a reference to an object, returning 0 (null) is not valid! This function should ALWAYS return os.

commented: thanks +2

The default constructor is ambiguous with this constructor:

property(std::function<void(void)> GetFunction=NULL,std::function<T(void)> SetFunction=NULL)

because both can be called without any parameters. You should just remove the default constructor because that constructor with default parameters achieves the same result (setting both functions to null) when called without parameters.

The property<int> Age(&getage,&setage); line gives an error because both getage and setage are member functions, not free functions. You need to create pointers to member functions for that. Like this:

test() : Name(), Age(std::bind(&test::getage, this), std::bind(&test::setage, this))
{
    //nothing
}

property<string> Name;
property<int> Age;

That's that.

sorry, but i continue with some errors :(

#include <iostream>
#include <functional>

using namespace std;

template <typename T>
class property
{
private:
    T PropertyValue;
    std::function<T(void)> getf;
    std::function<void(T)> setf;
public:

    property(T value)
    {
        PropertyValue=value;
    };

    property(std::function<void(void)> GetFunction=nullptr,std::function<T(void)> SetFunction=nullptr)
    {
        setf=SetFunction;//error
        getf=GetFunction;
    }

    property& operator=(T value)
    {
        if(getf==nullptr || setf==nullptr)
            PropertyValue=value;
        else
            setf(value);
        return *this;
    }

    T& operator=(property value)
    {
        if(value.getf==nullptr || value.setf==nullptr)
            return value.PropertyValue;
        else
            return value.getf();
    }

    friend ostream& operator<<(ostream& os, const property& dt)
    {
        if(dt.getf==nullptr && dt.setf==nullptr)
            os << dt.PropertyValue;
        else if (dt.getf!=nullptr)
            os << dt.getf();
        return os;
    }

    friend istream& operator>>(istream &input,property &dt)
    {
        if(dt.getf==nullptr && dt.setf==nullptr)
            input << dt.PropertyValue;
        else if (dt.setf!=nullptr)
            input << dt.setf();
        return input;
    }
};

class test
{
private:
    int age;
    int getage()
    {
        return age;
    }
    void setage(int value)
    {
        age=value;
    }
public:

    test():Name(), Age(std::bind(&test::getage, this), std::bind(&test::setage, this))//error
    {
        //nothing
    }
    property<string> Name;
    property<int> Age;

};

test a;

int main()
{
    a.Name="joaquim "  "Miguel";
    a.Age=10+15;
    cout << a.Age << endl;
    cout << "what is your name?\n";
    cin >> a.Age;
    cout << "your name is: " << a.Age << endl;
    return 0;
}

error:
- no matching function for call to 'property<int>::property(std::_Bind_helper<false, int (test::*)(), test* const>::type, std::_Bind_helper<false, void (test::*)(int), test* const>::type)'
- no match for 'operator=' (operand types are 'std::function<void(std::basic_string<char>)>' and 'std::function<std::basic_string<char>()>')

theres more errors :(

It's a typo, you had this:

property(std::function<void(void)> GetFunction=nullptr,std::function<T(void)> SetFunction=nullptr)

It should be this:

property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr)

Notice the first parameter should be std::function<T(void)>, not std::function<void(void)>, and similarly for the second one.

And the bind expression for the setter should be:

test() : Name(), 
         Age(std::bind(&test::getage, this),
             std::bind(&test::setage, this, _1))
{
commented: thanks for all +2

i belive these isn't correct too:

friend istream& operator>>(istream &input,property &dt)
    {
        if(dt.getf==nullptr && dt.setf==nullptr)
            input << dt.PropertyValue;
        else if (dt.setf!=nullptr)
            input << dt.setf() //maybe these isn't completed\corrected
        return input;
    }

yes... i continue with some errors :(

anotherthing: when i'm logined, can i recive the mail notification? i'm asking these, because i'm not reciving when i'm logined :(

I think that function should just be this:

friend istream& operator>>(istream &input, property &dt)
{
    input >> dt.PropertyValue;
    if (dt.setf!=nullptr)
        dt.setf(dt.PropertyValue);
    return input;
}

sorry, but that variable is used, only, when i don't need the getf and setf

Well, that variable is there all the time, whether you "need" it or not. So, you might as well use it.

now the code give me 2 problems:
1 - the '_1' ins't defined, so i use '1';
2 - the value is showed '1', but seems not be setted:(

#include <iostream>
#include <functional>

using namespace std;

template <typename T>
class property
{
private:
    T PropertyValue;
    std::function<T(void)> getf;
    std::function<void(T)> setf;
public:

    property(T value)
    {
        PropertyValue=value;
    };

    property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr)
    {
        setf=SetFunction;
        getf=GetFunction;
    }

    property& operator=(T value)
    {
        if(getf==nullptr || setf==nullptr)
            PropertyValue=value;
        else
            setf(value);
        return *this;
    }

    T& operator=(property value)
    {
        if(value.getf==nullptr || value.setf==nullptr)
            return value.PropertyValue;
        else
            return value.getf();
    }

    friend ostream& operator<<(ostream& os, const property& dt)
    {
        if(dt.getf==nullptr && dt.setf==nullptr)
            os << dt.PropertyValue;
        else if (dt.getf!=nullptr)
            os << dt.getf();
        return os;
    }

    friend istream& operator>>(istream &input, property &dt)
    {
        input >> dt.PropertyValue;
        if (dt.setf!=nullptr)
            dt.setf(dt.PropertyValue);
        return input;
    }
};

class test
{
private:
    int age;
    int getage()
    {
        return age;
    }
    void setage(int value)
    {
        age=value;
    }
public:

    test() : Name(), Age(std::bind(&test::getage, this),std::bind(&test::setage, this, 1))
    {

    }
    property<string> Name;//can i inicializate them here?
    property<int> Age;

};

test a;

int main()
{
    a.Name="joaquim "  "Miguel";
    a.Age=10+15;
    cout << a.Age << endl;
    cout << "what is your name?\n";
    cin >> a.Age;
    cout << "your name is: " << a.Age << endl;
    return 0;
}

see the comment, please or we left for later(after the problems) ;)

Sorry about that placeholder _1, it is in the namespace std::placeholders. So, it should be std::placeholders::_1 instead of just _1. The error was due to me being more used to using the Boost version of bind. You can see this docs on bind.

commented: thanks for all +0

thanks for all... realy.. thanks
for finish i need the property inicialization:
from:

test(): Name(), Age(std::bind(&test::getage, this),std::bind(&test::setage, this, std::placeholders::_1))
    {

    }
    property<string> Name ;//i need be possible inicializate them here
    property<int> Age;

to:

test()
    {

    }
    property<string> Name ;//i need be possible inicializate them here
    property<int> Age(std::bind(&test::getage, this),std::bind(&test::setage, this, std::placeholders::_1));

how can i change that?
i'm using '()', but can be '{}' or other

You don't need that. There is no reason to use that method for initializing data members, and I'm not even sure it's possible with this kind of stuff, I think you can only initialize with a constant expression, and this is not possible in this case. In any case, this method of data member initialization is merely syntax sugar, it provides nothing special that is "necessary" for anything.

i did that ;)
heres the entire with 1 nice macro:

#include <iostream>
#include <functional>

using namespace std;

template <typename T>
class property
{
private:
    T PropertyValue;
    std::function<T(void)> getf;
    std::function<void(T)> setf;
public:

    property(T value)
    {
        PropertyValue=value;
    };

    property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr)
    {
        setf=SetFunction;
        getf=GetFunction;
    }

    property& operator=(T value)
    {
        if(getf==nullptr || setf==nullptr)
            PropertyValue=value;
        else
            setf(value);
        return *this;
    }

    T& operator=(property value)
    {
        if(value.getf==nullptr || value.setf==nullptr)
            return value.PropertyValue;
        else
            return value.getf();
    }

    friend ostream& operator<<(ostream& os, const property& dt)
    {
        if(dt.getf==nullptr && dt.setf==nullptr)
            os << dt.PropertyValue;
        else if (dt.getf!=nullptr)
            os << dt.getf();
        return os;
    }

    friend istream& operator>>(istream &input, property &dt)
    {
        input >> dt.PropertyValue;
        if (dt.setf!=nullptr)
            dt.setf(dt.PropertyValue);
        return input;
    }

    friend istream &getline(istream &in, property &dt)
    {
        getline(in, dt.PropertyValue);
        if (dt.setf!=nullptr)
            dt.setf(dt.PropertyValue);
        return in;
    }
};

#define Property(TypeName,PropertyName,getfunction, setfunction);   property<TypeName> PropertyName{std::bind(&getfunction, this),std::bind(&setfunction, this, std::placeholders::_1)};

class test
{
private:
    int age;
    int getage()
    {
        return age;
    }
    void setage(int value)
    {
        age=value;
    }
public:

    test()
    {

    }
    property<string> Name ;
    Property(int, Age,test::getage, test::setage);

};

test a;

int main()
{
    a.Name="joaquim "  "Miguel";
    a.Age=10+15;
    cout << a.Age << endl;
    cout << "what is your name?\n";
    //getline(cin, a.Name);
    cin >> a.Age;
    cout << "your name is: " << a.Age << endl;
    return 0;
}

see how i declare the Age property and the macro defined.
true that you help me very for fix these code, but what you think about it? and the macro?

1 question: when i do:

a.

is showed a list of class members. but the Age isn't showed. why isn't showed? it's because is a macro?

see how i declare the Age property and the macro defined.

Well, there is an error in your MACRO definition, you have an extra semi-colon. It should be:

#define Property(TypeName,PropertyName,getfunction, setfunction) \
  property<TypeName> PropertyName{std::bind(&getfunction, this),std::bind(&setfunction, this, std::placeholders::_1)};

why isn't showed? it's because is a macro?

Yes. Code completion tools do not, in general, expand macros (they can't risk opening that can of worms). So, they won't show stuff that is buried in a macro.

but what you think about it? and the macro?

I think the macro is completely pointless. And you certainly should never use a name like Property for a MACRO. It should something along the lines of CAMBALINHO_LIBRARY_CREATE_PROPERTY instead. Macro names must obey two conventions: (1) all written in UPPER_CASE_LETTERS, and (2) be prefixed with a unique name that identifies your library. I cannot condone any macro use that does not obey these rules. And the name Property is pretty much the worst possible name for a macro that I could imagine.

sorry, but why the macros must be in upper case letters and not like other names?
i like the names like functions or others(that's why i did 'Property')... but the upper case way make easy the programmer see what he is using.
to be honest, i belive the macros are very cool and can avoid us very things to write(sorry it's my opinion).
i don't like very big things for do the same thing, that's why i use macros ;)
i think you remember the other property code, i must use 2 lines of code(1 for constructor and other outside of it)... but, if i can, i love be more simple, and i think these is a nice code for be simple.

thanks for all

MACROs must be upper-case because this is the convention throughout the programming world of C and C++ and related languages. Basically, MACROs have a number of quirky things about them, and are riddled with pitfalls, and therefore, all programmers want to know, just by looking at something, that it is a MACRO, and thus, need to pay special attention when using it. MACROs allow you to do all sorts of crazy stuff (and that's why they are sometimes the only option), but that also means that they make it easy to cause really weird and hard-to-find errors. So, whenever you use a MACRO for something, you have to put up a huge red flag to announce to the world that this is a MACRO, and the convention for doing that is to use the ALL_UPPER_CASE convention.

and not like other names?

MACROs are nothing like other things (functions, variables, classes, etc.) because they are not handled by the compiler but by the pre-processor. MACROs (or simpler #define tokens) are basically markers for the pre-processor that passes over the code and does a kind of "find-and-replace" for all instances where it finds the MACRO name. The problem is that the pre-processor does not consider context, it does not do scope-based name lookups, it ignores namespaces and scopes, i.e., it is a completely brainless find-and-replace tool. This means that the compiler does not care if the MACRO is used in the right place or if the macro name might refer to something else (e.g., what if someone creates a class or function or variable called "Property", the pre-processor will do the substitution regardless). This means that you have to be extra careful not to give your MACROs a name that could clash with anything else, in other words, you have to give it an extremely unique name, such as CAMBALINHO_LIBRARY_CREATE_PROPERTY, to make sure nothing else, anywhere in the world, is likely to have the same name.

When you do class names and function names, you don't have that problem because your classes and functions would be within the namespace of your library (by using namespace cambalinho { /* all code here */ }; around all your declarations). And, classes, functions and variables respect scoping rules and are looked-up in a "smart" fashion that takes into account the context in which you invoke the names.

can avoid us very things to write

They can also hide a lot of things. You have to worry about balancing the amount of things you hide and the amount of things you show. If you hide too much complicated stuff within MACROs, people will be very annoyed because the code because cryptic and confusing. You have to understand that C++ programmers are good at reading C++ code. What may seem cryptic to you (with your limited experience with C++), are actually very straight-forward for C++ programmers to see, read and write. And vice versa, what may seem to you as being simpler, nicer code might actually be seen as cryptic and annoying to experienced programmers. Personally, the version with the MACRO is definitely more cryptic to me.

If you write code for others (e.g., writing library code), then your personal preferences come second. You have to write for your audience, not for yourself. And you have to understand your audience (what are they used to, what conventions they expect, etc.).

commented: thanks for all +0

thanks for all

let me ask anotherthing about these and other macro:

#define event(eventname, ... ) std::function<void(__VA_ARGS__ )> eventname

using these macro in a class and do:

classobjectname.

members builded by that macro are showed in list, but why not the property macro?
they are both '#define'

i found 1 error with code:

#ifndef PROPERTY_H
#define PROPERTY_H

#include <iostream>
#include <functional>

using namespace std;

template <typename T>
class property
{
private:
    T PropertyValue;
    std::function<T(void)> getf;
    std::function<void(T)> setf;
public:

    property(T value)
    {
        PropertyValue=value;
    };

    property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr)
    {
        setf=SetFunction;
        getf=GetFunction;
    }

    property& operator=(T value)
    {
        if(getf==nullptr || setf==nullptr)
            PropertyValue=value;
        else
            setf(value);
        return *this;
    }

    T& operator=(property value)
    {
        if(value.getf==nullptr || value.setf==nullptr)
            return value.PropertyValue;
        else
            return value.getf();
        return *this;
    }


    friend ostream& operator<<(ostream& os, const property& dt)
    {
        if(dt.getf==nullptr && dt.setf==nullptr)
            os << dt.PropertyValue;
        else if (dt.getf!=nullptr)
            os << dt.getf();
        return os;
    }

    friend istream& operator>>(istream &input, property &dt)
    {
        input >> dt.PropertyValue;
        if (dt.setf!=nullptr)
            dt.setf(dt.PropertyValue);
        return input;
    }

    friend istream &getline(istream &in, property &dt)
    {
        getline(in, dt.PropertyValue);
        if (dt.setf!=nullptr)
            dt.setf(dt.PropertyValue);
        return in;
    }
};

template<typename T, typename Fnc1_t, typename Fnc2_t, typename classthis>
property<T> GetProperty(Fnc1_t Getter, Fnc2_t Setter, classthis clsthis)
{
    return property<T>(std::bind(Getter, clsthis), std::bind(Setter, clsthis, std::placeholders::_1));
}

#define PROPERTY(TypeName,PropertyName,getfunction, setfunction) \
property<TypeName> PropertyName{std::bind(&getfunction, this),std::bind(&setfunction, this, std::placeholders::_1)}
#endif //PROPERTY_H

and the main.cpp:

#include <iostream>
#include "property.h"
#include <sstream>

using namespace std;

class test
{
public:
    int x;
    int y;
    int getx()
    {
        return x;
    }
    void setx(int value)
    {
        x=value;
    }
    int gety()
    {
        return y;
    }
    void sety(int value)
    {
        y=value;
    }
    std::string go()
    {
        std::ostringstream out;
        out << '(' << x << ',' << y << ')' << ':' << x + y << '\n';
        return(out.str());
    }
public:

    test()
    {

    }
    PROPERTY(int, X,test::getx, test::setx);
    PROPERTY(int, Y,test::gety, test::sety);
};

int main()
{
    test a;
    cout << "what is the x?\n";
    cin >> a.X;
    cout << "what is the y?\n";
    cin >> a.Y;
    cout << "\nhere is the result:\n" << a.go() << '\n';
    test b = a;
    cout << b.x << "\t" << b.y << endl;
    cout << "what is a different x?\n";
    cin >> b.X;
    cout << "what is a different y?\n";
    cin >> b.Y;
    cout << "\nhere is the result:\n" << b.go() << '\n';
    cout << "\noh wait, no; here is the result:\n" << a.go() << '\n';
    return 0;
}

seems the property code isn't reciving the right class instance pointer :(
what you can tell me?

when i do:

test b = a;

i copy everything in 'a' to 'b'. even the 'this'...
now i know the problem is my property class.(i did a test just with the class test and works fine).
so the problem is when the compiler copy my property class(assignment operator... maybe overloading property &operator=(property &instancename).
but i need more advices, please.

problem resolved... for resolve it, i did a Copy Constructor, but works only between properties and not others class's. so, in class test, i did another constructor and works fine ;)
see the entire code:

//porperty.h
/*
- how use create 1 property with macro:

      PROPERTY(TypeName,PropertyName,getfunction, setfunction)

- never forget to do 1 Copy Constructor inside of class's, that uses the property,
         for avoid copy the 'this' value\adress and use a diferent memory adress
*/

#ifndef PROPERTY_H
#define PROPERTY_H

#include <iostream>
#include <functional>

using namespace std;

template <typename T>
class property
{
private:
    T PropertyValue;
    std::function<T(void)> getf;
    std::function<void(T)> setf;
public:

    property(const T value)
    {
        this->getf=nullptr;
        this->setf=nullptr;
        this->PropertyValue=value;
    };

    property(const property &value)
    {
        PropertyValue=value.PropertyValue;
    };

    property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr)
    {
        this->setf=SetFunction;
        this->getf=GetFunction;
    }

    property& operator=(const T &value) const
    {
        if(this->getf==nullptr || this->setf==nullptr)
            this->PropertyValue=value;
        else
            this->setf(value);
        return *this;
    }

    T& operator=(const property &value) const
    {
        if(value.getf==nullptr || value.setf==nullptr)
            return value.PropertyValue;
        else
            return value.getf();
        return *this;
    }

    friend ostream& operator<<(ostream& os, property& dt)
    {
        if(dt.getf==nullptr && dt.setf==nullptr)
            os << dt.PropertyValue;
        else if (dt.getf!=nullptr)
            os << dt.getf();
        return os;
    }

    friend istream& operator>>(istream &input, property &dt)
    {
        input >> dt.PropertyValue;
        if (dt.setf!=nullptr)
            dt.setf(dt.PropertyValue);
        return input;
    }

    friend istream &getline(istream &in, property &dt)
    {
        getline(in, dt.PropertyValue);
        if (dt.setf!=nullptr)
            dt.setf(dt.PropertyValue);
        return in;
    }
};

template<typename T, typename Fnc1_t, typename Fnc2_t, typename classthis>
property<T> GetProperty(Fnc1_t Getter, Fnc2_t Setter, classthis clsthis)
{
    return property<T>(std::bind(Getter, clsthis), std::bind(Setter, clsthis, std::placeholders::_1));
}

#define PROPERTY(TypeName,PropertyName,getfunction, setfunction) \
property<TypeName> PropertyName{std::bind(&getfunction, this),std::bind(&setfunction, this, std::placeholders::_1)}

#endif //PROPERTY_H

//main.cpp
#include <iostream>
#include <sstream>
#include "property.h"

using namespace std;

class test
{
public:
    int x;
    int y;
    int getx()
    {
        return x;
    }
    void setx(int value)
    {
        x=value;
    }
    int gety()
    {
        return y;
    }
    void sety(int value)
    {
        y=value;
    }
    std::string go()
    {
        std::ostringstream out;
        out << '(' << x << ',' << y << ')' << ':' << x + y << '\n';
        return(out.str());
    }

    test()
    {

    }

    //the Copy constructor for avoid copy the 'this'
    test(const test &tea)
    {
        this->x=tea.x;
        this->y=tea.y;
    }

    PROPERTY(int, X, test::getx, test::setx);
    PROPERTY(int, Y, test::gety, test::sety);
};

int main()
{
    test a;
    cout << "what is the x?\n";
    cin >> a.X;
    cout << "what is the y?\n";
    cin >> a.Y;
    cout << "\nhere is the result:\n" << a.go() << '\n';
    test b = a;
    cout << "what is a different x?\n";
    cin >> b.X;
    cout << "what is a different y?\n";
    cin >> b.Y;
    cout << "\nhere is the result:\n" << b.go() << '\n';
    cout << "\noh wait, no; here is the result:\n" << a.go() << '\n';
    return 0;
}

for explain better the problem: when we do:

classname a;
classname b=a;

the 'this' is copy too, that's why the value was changed in 'a' and not in 'b'. when these cases happens, we must create our own Copy Constructor for avoid copy the 'this' ;)
i hope these exemple help more readers ;)

1 question: can i do the copy constructor in my property class or must be inside of property test?

i'm testing my property class for accept lambdas too:

/*properties

- how use create 1 property with macro:

      PROPERTY(TypeName,PropertyName,getfunction, setfunction)

- never forget to do 1 Copy Constructor inside of class's, that uses the property,
         for avoid copy the 'this' value\adress and use a diferent memory adress
*/
template <typename T>
class property
{
private:
    T PropertyValue;
    std::function<T(void)> getf;
    std::function<void(T)> setf;
public:

    property(const T value)
    {
        getf=nullptr;
        setf=nullptr;
        PropertyValue=value;
    };

    property(const property &value)  :  PropertyValue(value.PropertyValue) , getf(value.getf)
    {
    }

    property(std::function<T(void)> GetFunction=nullptr,std::function<void(T)> SetFunction=nullptr)
    {
        setf=SetFunction;
        getf=GetFunction;
    }

    property& operator=(const T &value)
    {
        PropertyValue=value;
        return *this;
    }

    property& operator=(const property &value)
    {
        PropertyValue = value.PropertyValue;
        getf = value.getf;

        return *this;
    }

    friend ostream& operator<<(ostream& os, property& dt)
    {
        if(dt.getf==nullptr && dt.setf==nullptr)
            os << dt.PropertyValue;
        else if (dt.getf!=nullptr)
            os << dt.getf();
        return os;
    }

    friend istream& operator>>(istream &input, property &dt)
    {
        input >> dt.PropertyValue;
        if (dt.setf!=nullptr)
            dt.setf(dt.PropertyValue);
        return input;
    }

    friend istream &getline(istream &in, property &dt)
    {
        getline(in, dt.PropertyValue);
        if (dt.setf!=nullptr)
            dt.setf(dt.PropertyValue);
        return in;
    }
};

template<typename T, typename Fnc1_t, typename Fnc2_t, typename classthis>
property<T> GetProperty(Fnc1_t Getter, Fnc2_t Setter, classthis clsthis)
{
    return property<T>(std::bind(Getter, clsthis), std::bind(Setter, clsthis, std::placeholders::_1));
}

#define PROPERTY(TypeName,PropertyName,getfunction, setfunction) \
property<TypeName> PropertyName{std::bind(&getfunction, *this),std::bind(&setfunction, *this, std::placeholders::_1)}

how i use it:

class k
{
private:
    string name;
public:
   property<string> Name([](){return name;}, [](string strname){name=strname;});

};

but i'm getting several 5 errors:
i belive that i mistake how build the lambda in 1 argument... please, anyone can tell me something?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.