, to me they look no different than overloaded constructors , can anybody please explain them to me in simple words ?
EXPLAIN STATE METHOD IN C++ OOP ?
Started by
pllaybuoy
, Nov 08 2012 02:07 PM
#1
Posted 08 November 2012 - 02:07 PM
, to me they look no different than overloaded constructors , can anybody please explain them to me in simple words ?
#2
Posted 08 November 2012 - 03:00 PM
Hey there pllaybuoy and welcome to GeeksToGo,
I hope I can provide you with an appropriate answer relating to your question...
If i am correct you want to better understand what a state method/function is in simple words?
This is better known as a "functor", which is basically a class/struct that can be called as a function; But this function is special being that it can hold state. So you are kind of correct in saying it "looks no different than a constructor", that's because you call it using its constructor, but what makes it different is when you call it a second time using its constructor it keeps the original state of the original object. Instead of looking at it as a constructor, think of it more as a parameter of a local function that changes a variable within your local class. But when the first object is created with its specific state, that state is kept the same until the object is destroyed.
In other words if you create the object which stores a variable int static_x = 10, then you call that object like it was a function and have it add what ever is in the parameter to the firstly set variable functor(int x), so functor(5) would output a value of 15.
So I really hope that simple description helps out, simply put a state method is a method that keeps state. hehe
Please let me know if I can better explain myself in some area of the text, or if you require any other help.
Peace Out
I hope I can provide you with an appropriate answer relating to your question...
If i am correct you want to better understand what a state method/function is in simple words?
This is better known as a "functor", which is basically a class/struct that can be called as a function; But this function is special being that it can hold state. So you are kind of correct in saying it "looks no different than a constructor", that's because you call it using its constructor, but what makes it different is when you call it a second time using its constructor it keeps the original state of the original object. Instead of looking at it as a constructor, think of it more as a parameter of a local function that changes a variable within your local class. But when the first object is created with its specific state, that state is kept the same until the object is destroyed.
In other words if you create the object which stores a variable int static_x = 10, then you call that object like it was a function and have it add what ever is in the parameter to the firstly set variable functor(int x), so functor(5) would output a value of 15.
So I really hope that simple description helps out, simply put a state method is a method that keeps state. hehe
Please let me know if I can better explain myself in some area of the text, or if you require any other help.
Peace Out
#3
Posted 09 November 2012 - 03:24 AM
Hey hi thank you , and I did a little research myself and found that it is actually programming paradigm , maybe you are talking about that STATE MACHINE thing , no ?
and oops , sorry I meant state MEMBER not method , it could be any variable that defines the state of a class or anything its just a programming paradigm not part of any language
and oops , sorry I meant state MEMBER not method , it could be any variable that defines the state of a class or anything its just a programming paradigm not part of any language
Similar Topics
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users