Jump to content
  • Advertisement
Sign in to follow this  
_orm_

Feature Suggestion: Disable Default Constructor

This topic is 2562 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

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.


Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!