Results 1 to 4 of 4

Thread: Parameterized constructors for structs?

  1. #1
    shaver is offline Registered User
    Join Date
    May 2003
    Posts
    35

    Parameterized constructors for structs?

    It would be nice if slice2cpp would generate a basic parameterized constructor for structs, such that
    Code:
    struct Position {
        int x;
        int y;
        int z;
    }
    could be instantiated in C++ with something like
    Code:
    Position p(1, 2, 3);
    instead of having to spell it out field-by-field as
    Code:
    Position p;
    p.x = 1;
    p.y = 2;
    p.z = 3;
    Mike

  2. #2
    michi's Avatar
    michi is offline Registered User
    Name: Michi Henning
    Organization: Triodia Technologies
    Project: I have a passing interest in Ice :-)
    Join Date
    Feb 2003
    Location
    Brisbane, Australia
    Posts
    1,055

    Re: Parameterized constructors for structs?

    Originally posted by shaver
    It would be nice if slice2cpp would generate a basic parameterized constructor for structs
    I considered this for structures, non-abstract classes, and exceptions, but the gain seems to be minor: we'd end up generating additional code for every structure, class, and exception when, in many cases, that code would never be called and, therefore, would serve only to bloat the process and add to its working set size. So, on balance, I suspect that the minor added convenience isn't worth the penalty.

    Incidentally, because the structure members are public, you can statically initialize them:
    Code:
    struct Position p = { 1, 2, 3 };
    Cheers,

    Michi.

  3. #3
    shaver is offline Registered User
    Join Date
    May 2003
    Posts
    35

    Re: Re: Parameterized constructors for structs?

    Originally posted by michi
    I considered this for structures, non-abstract classes, and exceptions, but the gain seems to be minor: we'd end up generating additional code for every structure, class, and exception when, in many cases, that code would never be called and, therefore, would serve only to bloat the process and add to its working set size. So, on balance, I suspect that the minor added convenience isn't worth the penalty.
    I guess I was thinking of inline constructors, such that they would become simply a bit of syntactic sugar, and wouldn't incur any penalty for programs that didn't use them. I do appreciate your desire to keep the generated code's size down, though!
    Incidentally, because the structure members are public, you can statically initialize them:
    Code:
    struct Position p = { 1, 2, 3 };
    Very true, though using that sort of static initialization as part of an expression (for me, commonly, function arguments) makes for some ugly typing.

    Mike

  4. #4
    michi's Avatar
    michi is offline Registered User
    Name: Michi Henning
    Organization: Triodia Technologies
    Project: I have a passing interest in Ice :-)
    Join Date
    Feb 2003
    Location
    Brisbane, Australia
    Posts
    1,055

    Re: Re: Re: Parameterized constructors for structs?

    Originally posted by shaver
    I guess I was thinking of inline constructors, such that they would become simply a bit of syntactic sugar, and wouldn't incur any penalty for programs that didn't use them. I do appreciate your desire to keep the generated code's size down, though!
    Well, even if the constructor is defined in-line, there is no guarantee that the compiler will actually inline it, so it may still result in dead code. In fact, with older linkers that do not eliminate duplicate inline functions, having the inline constructor can result in getting a separate copy in every object file that includes the header file in which the structure is defined (although, I suspect that pretty much all linkers these days are smart enough to eliminate the duplicates).

    Very true, though using that sort of static initialization as part of an expression (for me, commonly, function arguments) makes for some ugly typing.
    Yes, that doesn't look too pretty... If you are really keen on having the constructor, you can derive a struct from the one that is generated by the slice2cpp compiler and define the constructor for that:
    Code:
    struct MyPosition : public Position {
        MyPosition(int x, int y, int z) : Position(x, y, z) {}
    };
    
    foo(MyPosition(a, b, c));
    I think that, overall, this solution is preferable, given that it is trivial to implement.

    Cheers,

    Michi.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 4
    Last Post: 10-16-2009, 06:51 PM
  2. Source Code Generator Option for Constructors
    By plissak in forum Comments
    Replies: 1
    Last Post: 07-21-2009, 12:01 PM
  3. Mapping Slice structs to C# structs
    By kwaclaw in forum Comments
    Replies: 4
    Last Post: 09-10-2007, 10:42 PM
  4. Replies: 1
    Last Post: 08-07-2005, 07:00 PM
  5. Replies: 2
    Last Post: 01-04-2005, 10:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •