Thread: about properties

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    451

    about properties

    i have 1 nice code for properties, but i want a direct code(with lambdas):

    property <string> propertyname(std::function<string(void)>,std::func tion<void(string));
    can anyone advice me doing these code?
    i can do the code, but the overloading operators make me 'crazy' to understand them correctly

  2. #2
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    finally i have 1 code to show you:
    Code:
    template <typename T>
    class property
    {
    private:
        std::function<void(void)> getf;
        std::function<void(void)> setf;
    public:
        property(std::function<void(void)> GetFunction,std::function<T(void)> SetFunction)
        {
            setf=SetFunction;
            getf=GetFunction;
        }
        property& operator=(T value)
        {
            SetFunction(value);
        }
    
        T& operator=(property value)
        {
            return GetFunction();
        }
    };
    but i'm getting 1 error now:
    "error: there are no arguments to 'GetFunction' that depend on a template parameter, so a declaration of 'GetFunction' must be available [-fpermissive]"
    so what i'm doing wrong?

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    i found my error lol
    Code:
    template <typename T>
    class property
    {
    private:
        std::function<void(void)> getf;
        std::function<void(void)> setf;
    public:
        property(std::function<void(void)> GetFunction,std::function<T(void)> SetFunction)
        {
            setf=SetFunction;
            getf=GetFunction;
        }
        property& operator=(T value)
        {
            setf(value);
        }
    
        T& operator=(property value)
        {
            return getf();
        }
    };
    now i will test it
    but can anyone tell me about the overloading operator = ?
    i'm asking, because i always fail with it

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    true it's confused and i did that lol
    can anyone advice, please?

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    ok.. now the code works for a standar properties(like normal variables):
    Code:
    #include <iostream>
    #include <functional>
    
    using namespace std;
    
    template <typename T>
    class property
    {
    private:
        T PropertyValue;
        std::function<void(void)> getf=NULL;
        std::function<void(T a)> setf=NULL;
    public:
        property()
        {
            //nothing
        };
    
        property(std::function<void(void)> GetFunction,std::function<T(void)> SetFunction)
        {
            setf=SetFunction;
            getf=GetFunction;
        }
    
        property& operator=(T value)
        {
            if(setf==NULL)
                PropertyValue=value;
            else
                setf(value);
            return *this;
        }
    
        T& operator=(property value)
        {
            if(getf==NULL)
                return PropertyValue;
            else
                return getf();
        }
    
        friend ostream& operator<<(ostream& os, const property& dt)
        {
            os << dt.PropertyValue;
            return os;
        }
    
        friend istream& operator>>(istream &input,property &dt)
        {
            input>>dt.PropertyValue;
            return input;
        }
    };
    
    class test
    {
    public:
        property<string> Name;
        property<int> Age;
    };
    
    test a;
    int main()
    {
        a.Name="joaquim "  "Miguel";
        a.Age=10+15;
        cout << a.Name << endl;
        cout << "what is your name?\n";
        cin >> a.Name;
        cout << "your name is: " << a.Name << endl;
        return 0;
    }
    my problem is with functions:
    Code:
    #include <iostream>
    #include <functional>
    
    using namespace std;
    
    template <typename T>
    class property
    {
    private:
        T PropertyValue;
        std::function<void(void)> getf=NULL;
        std::function<void(T a)> setf=NULL;
    public:
        property()
        {
            //nothing
        };
    
        property(std::function<void(void)> GetFunction,std::function<T(void)> SetFunction)
        {
            setf=SetFunction;
            getf=GetFunction;
        }
    
        property& operator=(T value)
        {
            if(setf==NULL)
                PropertyValue=value;
            else
                setf(value);
            return *this;
        }
    
        T& operator=(property value)
        {
            if(getf==NULL)
                return PropertyValue;
            else
                return getf();
        }
    
        friend ostream& operator<<(ostream& os, const property& dt)
        {
            os << dt.PropertyValue;
            return os;
        }
    
        friend istream& operator>>(istream &input,property &dt)
        {
            input>>dt.PropertyValue;
            return input;
        }
    };
    
    void sethi(string b)
    {
        b = b + " hi";
    }
    
    string gethi()
    {
        return "hello";
    }
    
    class test
    {
    public:
        property<string> Name(&sethi,&gethi);
        property<int> Age;
    };
    
    test a;
    int main()
    {
        a.Name="joaquim "  "Miguel";
        a.Age=10+15;
        cout << a.Name << endl;
        cout << "what is your name?\n";
        cin >> a.Name;
        cout << "your name is: " << a.Name << endl;
        return 0;
    }
    i get 6 errors and several notes
    the 1st 2 errors are:
    "error: expected identifier before '&' token"
    in these line:
    Code:
    property<string> Name(&sethi,&gethi);
    can anyone advice me where i'm wrong?
    Last edited by joaquim; 02-01-2014 at 03:13 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't see that line in your code. If you want to initialize a member variable, that's not where the initializer goes. (That's what the constructor is for.)

  7. #7
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by tabstop View Post
    I don't see that line in your code. If you want to initialize a member variable, that's not where the initializer goes. (That's what the constructor is for.)
    on second example. and i use 2 construtors.

    (i will fix 1 thing on code)

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you can't count all the way up to zero, we've got bigger problems. A constructor would look like
    Code:
    test::test(parameters go here) : initializers go here {statements go here};

  9. #9
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by tabstop View Post
    If you can't count all the way up to zero, we've got bigger problems. A constructor would look like
    Code:
    test::test(parameters go here) : initializers go here {statements go here};
    now i understand what you mean:
    Code:
    #include <iostream>
    #include <functional>
    
    using namespace std;
    
    template <typename T>
    class property
    {
    private:
        T PropertyValue;
        std::function<void(void)> getf;
        std::function<void(T a)> setf;
    public:
        property()
        {
            //nothing
        };
    
        property(std::function<void(void)> GetFunction,std::function<T(void)> SetFunction) : getf(NULL),getf(NULL)
        {
            setf=SetFunction;
            getf=GetFunction;
        }
    
        property& operator=(T value)
        {
            if(setf==NULL)
                PropertyValue=value;
            else
                setf(value);
            return *this;
        }
    
        T& operator=(property value)
        {
            if(getf==NULL)
                return PropertyValue;
            else
                return getf();
        }
    
        friend ostream& operator<<(ostream& os, const property& dt)
        {
            os << dt.PropertyValue;
            return os;
        }
    
        friend istream& operator>>(istream &input,property &dt)
        {
            input>>dt.PropertyValue;
            return input;
        }
    };
    
    void sethi(string b)
    {
        b = b + " hi";
    }
    
    string gethi()
    {
        return "hello";
    }
    
    class test
    {
    public:
        property<string> Name(&sethi,&gethi);
        property<int> Age;
    };
    
    test a;
    int main()
    {
        a.Name="joaquim "  "Miguel";
        a.Age=10+15;
        cout << a.Name << endl;
        cout << "what is your name?\n";
        cin >> a.Name;
        cout << "your name is: " << a.Name << endl;
        return 0;
    }
    but i continue with same errors

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you understand what I meant, then why are there still no constructors for test?

  11. #11
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by tabstop View Post
    If you understand what I meant, then why are there still no constructors for test?
    Code:
    class test
    {
    public:
        test() : Age(30),Name("Joaquim")
        {
            ;
        }
        property<string> Name(&sethi,&gethi);
        property<int> Age;
    };
    
    test a;

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I appreciate that you are new, but you are doing yourself no favors with this "HURRY RESPOND!" style of posting.

    Read over the thread again, you have what you need to fix your code if you'd take a few minutes to actually apply what people are telling you.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  13. #13
    Registered User
    Join Date
    Aug 2013
    Posts
    451
    Quote Originally Posted by phantomotap View Post
    O_o

    I appreciate that you are new, but you are doing yourself no favors with this "HURRY RESPOND!" style of posting.

    Read over the thread again, you have what you need to fix your code if you'd take a few minutes to actually apply what people are telling you.

    Soma
    sorry. i understand that i must initializate the values and do construtors, or i can get unexpected results. but i'm confuse how i can fix these code

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by joaquim View Post
    Code:
    class test
    {
    public:
        test() : Age(30),Name("Joaquim")
        {
            ;
        }
        property<string> Name(&sethi,&gethi);
        property<int> Age;
    };
    
    test a;
    I don't know how much booze you've gotten through this far on a Saturday night, but you haven't hit the Ballmer peak yet (or worse, you're past it already). Look at your constructor -- do you think property<string> has a constructor that takes a string argument? Why would you think that? And you cannot try to initialize a member variable at its declaration; that's what the initializer list of the constructor is for.

    Code:
    test::test(take some information here, like sethi, gethi, and age) : initialize some member variables here, like Name(sethi, gethi), Age(age) {any other statements go here;}

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    And you cannot try to initialize a member variable at its declaration; that's what the initializer list of the constructor is for.
    Actually, not true. You are allowed to do that in C++11, which specifies the default value the member variables will have unless overriden by the initializer list.
    Anyway, a function object is not a function pointer. You cannot initialized to an int (which NULL is). But using the default constructor also creates a non-bound function object, so that should give you the same results.
    While you can get away with comparing a std::function with NULL, ideally you should not. Either test it in a boolean context (e.g. if(my_function_obj)) or with nullptr (e.g. if (my_function_obj != nullptr)).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DLL Properties @ Windows7 using g++
    By artur in forum C++ Programming
    Replies: 11
    Last Post: 05-30-2012, 06:41 AM
  2. No properties in c++?
    By MisterT in forum C++ Programming
    Replies: 19
    Last Post: 08-06-2006, 08:54 AM
  3. window properties
    By Warhawk in forum C++ Programming
    Replies: 9
    Last Post: 09-15-2005, 03:09 PM
  4. FPS properties.
    By DarkBrute in forum Game Programming
    Replies: 24
    Last Post: 02-03-2005, 07:41 PM