Limiting class instantiation and function calls

8 posts

Flag Post

I need to allow creating a particular class only from allowed classes.
For example:

public class Class1
public function Class1()
{
var class3:Class3 = new Class3(); //Error
}


public class Class2
public function Class2()
{
var class3:Class3 = new Class3(); //No Error
}

Any efficient way to do this?

 
Flag Post

Mmhhh… ¿Use packages and import them in the class you want? ¿Include the class in the same package (if you only want to use it with one class)?

 
Flag Post

Why?

 
Flag Post

Sounds like the “friends” feature of C++ that has spawned so many jokes.

In AS3, namespaces seem to provide similar functionality.

 
Flag Post

Does the class which you are attempting to initialise know which classes are allowed to initialise it? If so, you could just check against a list and chuck an error if they don’t match.

Although there’s probably a more clever solution, as others have already suggested.

 
Flag Post

That’s pretty much the point of access modifiers. Only a class within the same package can access internal classes.

 
Flag Post

Make the Class3 constructor take a password parameter and have it throw an error if the password isn’t correct. Don’t tell the password to anybody.

 
Flag Post
Originally posted by Drakim:

Make the Class3 constructor take a password parameter and have it throw an error if the password isn’t correct. Don’t tell the password to anybody.

But that could be bruteforced.
How about a GUID as a password?