Sign in to follow this  
_orm_

Feature Suggestion: Disable Default Constructor

Recommended Posts

_orm_    112
I have been having trouble lately keeping my constructors in order, and the compiler seems to like implementing the default constructor for my classes. Could it be possible to disable them? One thing I like in the new C++11 standard is the following:

[code]class NonCopyableClass
{
public:
NonCopyableClass(const NonCopyableClass& other) = delete; // This is inaccessible
};[/code]

The delete modifier allows us to create a noncopyable class without having to use a base class. So maybe for AngelScript we could do the following:
[code]class ClassWithoutDefaultConstructor
{
ClassWithoutDefaultConstructor() = null; // The default constructor can not be called and the compiler will complain.
};[/code]

The reason I bring this up is that I have spent a good portion of time debugging scripts because the compiler inserts a default constructor and I have no way to prevent it being called.

My work-around for the moment is this:
[code]
void ScriptCrash(const string& message)
{
asIScriptContext* ctx = asGetActiveContext();
if(!ctx) return;
ctx->SetException(string("SCRIPT CRASH: "+message).c_str());
}[/code]

And then registering it and inserting it wherever I want execution to be halted. However, a built-in solution would probably be cleaner and promote better design over this sort of hackish solution.

Share this post


Link to post
Share on other sites
Bismuth    105
I had a similar idea a while ago except that I would use an engine property to completely disable the use of default constructors (or destructors) in a script.

Share this post


Link to post
Share on other sites
_orm_    112
Well, how about we throw some out there? The obvious ones are the one I proposed above and another keyword that is used like the private modifier.

[code]class SomeClass
{
disabled SomeClass();
// or
delete SomeClass();
};[/code]

Anything else?

Share this post


Link to post
Share on other sites
immortius    100
remove Constructor()
noauto Constructor()
noop Constructor()
pleaseno Constructor()
:P

Or perhaps it could copy the behavior of some other languages and only automatically produce a default constructor if no other constructor is present.

Share this post


Link to post
Share on other sites
_orm_    112
[quote name='immortius' timestamp='1310383221' post='4833670']
Or perhaps it could copy the behavior of some other languages and only automatically produce a default constructor if no other constructor is present.
[/quote]

This.


Share this post


Link to post
Share on other sites
WitchLord    4677
[quote name='immortius' timestamp='1310383221' post='4833670']
Or perhaps it could copy the behavior of some other languages and only automatically produce a default constructor if no other constructor is present.
[/quote]

This is what I had planned so far. With the addition of adding a default constructor manually with the following syntax:

[code]
class T
{
T() default; // implement the default constructor, even though there is another constructor
T(int) {}; // The existence of this non-default constructor would remove the default constructor
}
[/code]

The same thing with the copy constructor and the opAssign method.

Share this post


Link to post
Share on other sites
immortius    100
Sounds good, although would T() default do anything T() {} doesn't (besides act the same way as adding in the other default methods)?

Another feature that might be nice would be private constructors - primarily for having script classes that can only be created from the native end (possibly through a factory method).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this