Feature Suggestion: Disable Default Constructor

Started by
6 comments, last by immortius 12 years, 9 months ago
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:

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


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:
class ClassWithoutDefaultConstructor
{
ClassWithoutDefaultConstructor() = null; // The default constructor can not be called and the compiler will complain.
};


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:

void ScriptCrash(const string& message)
{
asIScriptContext* ctx = asGetActiveContext();
if(!ctx) return;
ctx->SetException(string("SCRIPT CRASH: "+message).c_str());
}


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.
Advertisement
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.
This is already on my to-do list. Though I've not decided upon the syntax yet.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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.

class SomeClass
{
disabled SomeClass();
// or
delete SomeClass();
};


Anything else?
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.

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


This.



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


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


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
}


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

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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).

This topic is closed to new replies.

Advertisement