[Closed] Struct private, public members
Hello again,
I was checking latetly some C++ tutorials and I saw a simple and cool techinque on how to “hide” your vars from your classes. He created a public “GET” functiion and through this, he was calling the private variables. For changing the value to this var he created a “SET” function.
I’ve tried to replicate it with structs in MAXscript and either I do something wrong or maxscript does allow public fuctions to access the private members.
Here is simple example
struct testStruct
(
public
fn getVar = myVar,
private
myVar = 1
)
test = testStruct()
test.getVar()
I did a quick search in the forum but I didn’t found any thread about that. Has anyone came across to that?
Thanks,
Nick
There is no encapsulation in maxscript, everything in a structure is public.
Thats not true, since Max 2010 you can declare struct members private. All members are public by default. You will get errors when you try to access private struct members outside the struct. Private struct membernames will show up in listener output though ( eg. when you do a getPropNames <yourstruct> ). But their values are still inaccessable outside your struct. AFAIK private struct members are completly hidden in encrypted scripts (including names), even in error messages
Of course the encapsulation is not as tight as in C++, but it can help alot, but it takes some time to get used to it’s quirks. eg. It’s best to organize your public and private sections in that order:
private member variables
....
public member variables
....
private member functions
....
public member functions
....
otherwhise you can run into pretty confusing interdependency problems
And note that the private and public keywords apply to EVERYTHING following after them.
So you can’t write the following when you only want memberC to become private, all members after the private keywords including all member functions will be private, untill the next public keyword after appears
[B]
...
memberA,
memberB,
private memberC,
memberD
...
[/B]
Anothe good coding advice is making your structs internas private , and write public accessor functions ( Set…/Get…) to it. Of course this should be counterbalanced with your performance requirements and at what rate those members will be accessed from outside
I don’t understand your question …
You simply write a public function which returns or changes the private member. Maxscript encapsulation is not that strong that this would throw an access error. A public function can modify/access a struct’s private members without problems. Only code outside the scope of the owning struct, can’t access private members
Edit:
i see on your first post’s example where the problem lies.You define a function which accesses a still not existing variable. This is exactly the reason for my strict struct declaration scheme i listed above. So golden rule for struct definition is:
- first come the member variables ( private and public ) and THEN come the member functions ( again private and public ). So you above example should be
[B]
struct testStruct
(
private
myVar = 1,
public
fn getVar = myVar
)
test = testStruct()
test.getVar()
1
-- that's the correct output
[/B]
I think that there is some kind o encapsulation… http://docs.autodesk.com/3DSMAX/14/ENU/MAXScript%20Help%202012/index.html?url=files/GUID-0D3592DD-2F20-4A41-B19F-544426B8186-454.htm,topicNumber=d28e151491
I have made some tests in the past and I was able to access the public vars from the private functions but not vice versa…
god! I feel so stupid… If you have seen my example, it returns undefined… and that’s because I’ve added the function after the var! I though that it was going to work just like when you setup the vars in a function.
Anywayz… it works now! Thanks!