AngelScript - Avoid implicit default constructor from running










0














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:



  1. Can I require a specific constructor to be called?

  2. Can I prevent a default constructor from running?

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










share|improve this question




























    0














    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:



    1. Can I require a specific constructor to be called?

    2. Can I prevent a default constructor from running?

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










    share|improve this question


























      0












      0








      0







      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:



      1. Can I require a specific constructor to be called?

      2. Can I prevent a default constructor from running?

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










      share|improve this question















      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:



      1. Can I require a specific constructor to be called?

      2. Can I prevent a default constructor from running?

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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 12 at 18:59

























      asked Nov 10 at 6:07









      Magnus Bull

      12911




      12911






















          1 Answer
          1






          active

          oldest

          votes


















          1














          1. In order to declare class and send the value you choose to constructor try:
            MyClass obj(5);


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






          share|improve this answer






















          • 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 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 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










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



          );













          draft saved

          draft discarded


















          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









          1














          1. In order to declare class and send the value you choose to constructor try:
            MyClass obj(5);


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






          share|improve this answer






















          • 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 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 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















          1














          1. In order to declare class and send the value you choose to constructor try:
            MyClass obj(5);


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






          share|improve this answer






















          • 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 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 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













          1












          1








          1






          1. In order to declare class and send the value you choose to constructor try:
            MyClass obj(5);


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






          share|improve this answer














          1. In order to declare class and send the value you choose to constructor try:
            MyClass obj(5);


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







          share|improve this answer














          share|improve this answer



          share|improve this answer








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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

          ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ

          Node.js puppeteer - Use values from array in a loop to cycle through pages