AngelScript - Avoid implicit default constructor from running
I'm currently testing some simple AngelScript stuff, and noticed something I find a bit strange when it comes to how objects are initialized from classes.
Let's say I define a class like this:
class MyClass
int i;
MyClass(int i)
this.i = i;
I can create an object of this class by doing this:
MyClass obj = MyClass(5);
However it seems I can also create an object by doing this:
MyClass obj;
The problem here is that obj.i becomes a default value as it is undefined.
Additionally, adding a default constructor to my class and a print function call in each one reveals that when I do MyClass obj = MyClass(5); BOTH constructors are called, not just the one with the matching parameter. This seems risky to me, as it could initialize a lot of properties unnecessarily for this "ghost" instance.
I can avoid this double-initialization by using a handle, but this seems more like a work-around rather than a solution:
MyClass@ obj = MyClass(5);
So my question sums up to:
- Can I require a specific constructor to be called?
- Can I prevent a default constructor from running?
- What's the proper way to deal with required parameters when creating objects?
Mind that this is purely in the AngelScript script language, completely separate from the C++ code of the host application. The host is from 2010 and is not open-source, and my knowledge of their implementation is very limited, so if the issue lies there, I can't change it.
class angelscript
add a comment |
I'm currently testing some simple AngelScript stuff, and noticed something I find a bit strange when it comes to how objects are initialized from classes.
Let's say I define a class like this:
class MyClass
int i;
MyClass(int i)
this.i = i;
I can create an object of this class by doing this:
MyClass obj = MyClass(5);
However it seems I can also create an object by doing this:
MyClass obj;
The problem here is that obj.i becomes a default value as it is undefined.
Additionally, adding a default constructor to my class and a print function call in each one reveals that when I do MyClass obj = MyClass(5); BOTH constructors are called, not just the one with the matching parameter. This seems risky to me, as it could initialize a lot of properties unnecessarily for this "ghost" instance.
I can avoid this double-initialization by using a handle, but this seems more like a work-around rather than a solution:
MyClass@ obj = MyClass(5);
So my question sums up to:
- Can I require a specific constructor to be called?
- Can I prevent a default constructor from running?
- What's the proper way to deal with required parameters when creating objects?
Mind that this is purely in the AngelScript script language, completely separate from the C++ code of the host application. The host is from 2010 and is not open-source, and my knowledge of their implementation is very limited, so if the issue lies there, I can't change it.
class angelscript
add a comment |
I'm currently testing some simple AngelScript stuff, and noticed something I find a bit strange when it comes to how objects are initialized from classes.
Let's say I define a class like this:
class MyClass
int i;
MyClass(int i)
this.i = i;
I can create an object of this class by doing this:
MyClass obj = MyClass(5);
However it seems I can also create an object by doing this:
MyClass obj;
The problem here is that obj.i becomes a default value as it is undefined.
Additionally, adding a default constructor to my class and a print function call in each one reveals that when I do MyClass obj = MyClass(5); BOTH constructors are called, not just the one with the matching parameter. This seems risky to me, as it could initialize a lot of properties unnecessarily for this "ghost" instance.
I can avoid this double-initialization by using a handle, but this seems more like a work-around rather than a solution:
MyClass@ obj = MyClass(5);
So my question sums up to:
- Can I require a specific constructor to be called?
- Can I prevent a default constructor from running?
- What's the proper way to deal with required parameters when creating objects?
Mind that this is purely in the AngelScript script language, completely separate from the C++ code of the host application. The host is from 2010 and is not open-source, and my knowledge of their implementation is very limited, so if the issue lies there, I can't change it.
class angelscript
I'm currently testing some simple AngelScript stuff, and noticed something I find a bit strange when it comes to how objects are initialized from classes.
Let's say I define a class like this:
class MyClass
int i;
MyClass(int i)
this.i = i;
I can create an object of this class by doing this:
MyClass obj = MyClass(5);
However it seems I can also create an object by doing this:
MyClass obj;
The problem here is that obj.i becomes a default value as it is undefined.
Additionally, adding a default constructor to my class and a print function call in each one reveals that when I do MyClass obj = MyClass(5); BOTH constructors are called, not just the one with the matching parameter. This seems risky to me, as it could initialize a lot of properties unnecessarily for this "ghost" instance.
I can avoid this double-initialization by using a handle, but this seems more like a work-around rather than a solution:
MyClass@ obj = MyClass(5);
So my question sums up to:
- Can I require a specific constructor to be called?
- Can I prevent a default constructor from running?
- What's the proper way to deal with required parameters when creating objects?
Mind that this is purely in the AngelScript script language, completely separate from the C++ code of the host application. The host is from 2010 and is not open-source, and my knowledge of their implementation is very limited, so if the issue lies there, I can't change it.
class angelscript
class angelscript
edited Dec 12 at 18:59
asked Nov 10 at 6:07
Magnus Bull
12911
12911
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
In order to declare class and send the value you choose to constructor try:
MyClass obj(5);To prevent using default constructor create it and use:
.
MyClass()
abort("Trying to create uninitialized object of type that require init parameters");
or
exit(1);
or
assert(1>2,"Trying to create uninitialized object of type that require init parameters");
or
engine.Exit();
in case that any of those is working in you environment.
declaring the constructor as private seems not to work in AS, unlike other languages.
I suppose the handle method is the way to go then. As for #2, perhaps the version of AngelScript being used here is too old, because I get an "Expected identifier" syntax error from setting a constructor asprivate. Thank you nonetheless; the handle does work.
– Magnus Bull
Dec 1 at 10:34
I fixed my answer. MyClass @ obj(5); didn't compile (I checked).
– arie
Dec 11 at 15:35
I fixed again my answer, after I checked that declaring of constructor as private realy don't work, as you said.
– arie
Dec 11 at 16:28
1
I tried the 4 solutions you added and none seemed to be recognized. They all gave syntax errors as if they were regular functions not matching a signature orenginenot being declared. If that's supposed to work, it probably relies on something not implemented in my host application.
– Magnus Bull
Dec 12 at 18:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236443%2fangelscript-avoid-implicit-default-constructor-from-running%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In order to declare class and send the value you choose to constructor try:
MyClass obj(5);To prevent using default constructor create it and use:
.
MyClass()
abort("Trying to create uninitialized object of type that require init parameters");
or
exit(1);
or
assert(1>2,"Trying to create uninitialized object of type that require init parameters");
or
engine.Exit();
in case that any of those is working in you environment.
declaring the constructor as private seems not to work in AS, unlike other languages.
I suppose the handle method is the way to go then. As for #2, perhaps the version of AngelScript being used here is too old, because I get an "Expected identifier" syntax error from setting a constructor asprivate. Thank you nonetheless; the handle does work.
– Magnus Bull
Dec 1 at 10:34
I fixed my answer. MyClass @ obj(5); didn't compile (I checked).
– arie
Dec 11 at 15:35
I fixed again my answer, after I checked that declaring of constructor as private realy don't work, as you said.
– arie
Dec 11 at 16:28
1
I tried the 4 solutions you added and none seemed to be recognized. They all gave syntax errors as if they were regular functions not matching a signature orenginenot being declared. If that's supposed to work, it probably relies on something not implemented in my host application.
– Magnus Bull
Dec 12 at 18:58
add a comment |
In order to declare class and send the value you choose to constructor try:
MyClass obj(5);To prevent using default constructor create it and use:
.
MyClass()
abort("Trying to create uninitialized object of type that require init parameters");
or
exit(1);
or
assert(1>2,"Trying to create uninitialized object of type that require init parameters");
or
engine.Exit();
in case that any of those is working in you environment.
declaring the constructor as private seems not to work in AS, unlike other languages.
I suppose the handle method is the way to go then. As for #2, perhaps the version of AngelScript being used here is too old, because I get an "Expected identifier" syntax error from setting a constructor asprivate. Thank you nonetheless; the handle does work.
– Magnus Bull
Dec 1 at 10:34
I fixed my answer. MyClass @ obj(5); didn't compile (I checked).
– arie
Dec 11 at 15:35
I fixed again my answer, after I checked that declaring of constructor as private realy don't work, as you said.
– arie
Dec 11 at 16:28
1
I tried the 4 solutions you added and none seemed to be recognized. They all gave syntax errors as if they were regular functions not matching a signature orenginenot being declared. If that's supposed to work, it probably relies on something not implemented in my host application.
– Magnus Bull
Dec 12 at 18:58
add a comment |
In order to declare class and send the value you choose to constructor try:
MyClass obj(5);To prevent using default constructor create it and use:
.
MyClass()
abort("Trying to create uninitialized object of type that require init parameters");
or
exit(1);
or
assert(1>2,"Trying to create uninitialized object of type that require init parameters");
or
engine.Exit();
in case that any of those is working in you environment.
declaring the constructor as private seems not to work in AS, unlike other languages.
In order to declare class and send the value you choose to constructor try:
MyClass obj(5);To prevent using default constructor create it and use:
.
MyClass()
abort("Trying to create uninitialized object of type that require init parameters");
or
exit(1);
or
assert(1>2,"Trying to create uninitialized object of type that require init parameters");
or
engine.Exit();
in case that any of those is working in you environment.
declaring the constructor as private seems not to work in AS, unlike other languages.
edited Dec 11 at 16:27
answered Nov 28 at 17:17
arie
366
366
I suppose the handle method is the way to go then. As for #2, perhaps the version of AngelScript being used here is too old, because I get an "Expected identifier" syntax error from setting a constructor asprivate. Thank you nonetheless; the handle does work.
– Magnus Bull
Dec 1 at 10:34
I fixed my answer. MyClass @ obj(5); didn't compile (I checked).
– arie
Dec 11 at 15:35
I fixed again my answer, after I checked that declaring of constructor as private realy don't work, as you said.
– arie
Dec 11 at 16:28
1
I tried the 4 solutions you added and none seemed to be recognized. They all gave syntax errors as if they were regular functions not matching a signature orenginenot being declared. If that's supposed to work, it probably relies on something not implemented in my host application.
– Magnus Bull
Dec 12 at 18:58
add a comment |
I suppose the handle method is the way to go then. As for #2, perhaps the version of AngelScript being used here is too old, because I get an "Expected identifier" syntax error from setting a constructor asprivate. Thank you nonetheless; the handle does work.
– Magnus Bull
Dec 1 at 10:34
I fixed my answer. MyClass @ obj(5); didn't compile (I checked).
– arie
Dec 11 at 15:35
I fixed again my answer, after I checked that declaring of constructor as private realy don't work, as you said.
– arie
Dec 11 at 16:28
1
I tried the 4 solutions you added and none seemed to be recognized. They all gave syntax errors as if they were regular functions not matching a signature orenginenot being declared. If that's supposed to work, it probably relies on something not implemented in my host application.
– Magnus Bull
Dec 12 at 18:58
I suppose the handle method is the way to go then. As for #2, perhaps the version of AngelScript being used here is too old, because I get an "Expected identifier" syntax error from setting a constructor as
private. Thank you nonetheless; the handle does work.– Magnus Bull
Dec 1 at 10:34
I suppose the handle method is the way to go then. As for #2, perhaps the version of AngelScript being used here is too old, because I get an "Expected identifier" syntax error from setting a constructor as
private. Thank you nonetheless; the handle does work.– Magnus Bull
Dec 1 at 10:34
I fixed my answer. MyClass @ obj(5); didn't compile (I checked).
– arie
Dec 11 at 15:35
I fixed my answer. MyClass @ obj(5); didn't compile (I checked).
– arie
Dec 11 at 15:35
I fixed again my answer, after I checked that declaring of constructor as private realy don't work, as you said.
– arie
Dec 11 at 16:28
I fixed again my answer, after I checked that declaring of constructor as private realy don't work, as you said.
– arie
Dec 11 at 16:28
1
1
I tried the 4 solutions you added and none seemed to be recognized. They all gave syntax errors as if they were regular functions not matching a signature or
engine not being declared. If that's supposed to work, it probably relies on something not implemented in my host application.– Magnus Bull
Dec 12 at 18:58
I tried the 4 solutions you added and none seemed to be recognized. They all gave syntax errors as if they were regular functions not matching a signature or
engine not being declared. If that's supposed to work, it probably relies on something not implemented in my host application.– Magnus Bull
Dec 12 at 18:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236443%2fangelscript-avoid-implicit-default-constructor-from-running%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown