Question about struct aspect of standard

Discussion in 'C Programming' started by mario.demiguel@gmail.com, Jan 4, 2006.

  1. Guest

    Can standard C structs have a constructor? Thanks in advance.
    , Jan 4, 2006
    #1
    1. Advertising

  2. rayw Guest

    <> wrote in message
    news:...
    > Can standard C structs have a constructor? Thanks in advance.


    Nope - if you're thinking of the C++ 'full monty' kinda thing.
    rayw, Jan 4, 2006
    #2
    1. Advertising

  3. Eric Sosman Guest

    wrote:

    > Can standard C structs have a constructor? Thanks in advance.


    C has no constructors, and no destructors either.
    You can initialize a struct at the point of declaration

    struct mystruct s = { 1, 42.0, "blue" };

    .... or you can assign to it from another pre-initialized
    struct:

    const struct mystruct proto_s = { 0, 0.0, "" };
    ...
    struct mystruct s;
    ...
    s = proto_s;

    .... or you can (manually) initialize its elements by
    calling a function:

    void init_mystruct(struct mystruct *);
    ...
    struct mystruct s;
    init_mystruct(&s);

    .... or you can write a function that serves as a "factory
    method," allocating and initializing a struct:

    struct mystruct *factory( ...args... );
    ...
    struct mystruct *sp, s;
    sp = factory( ...args... );
    if (sp == NULL) die();
    s = *sp;

    .... and there are lots more variations. However, nothing
    in C will cause any of this to happen automagically; you'll
    need to perform the machinations yourself.

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 4, 2006
    #3
  4. pemo Guest

    <> wrote in message
    news:...
    > Can standard C structs have a constructor? Thanks in advance.


    The answer is 'no', but it could be a 'may be' depending upon what it is
    you're trying to achieve. If it's a C++ clone, then, *not very easily* ...
    even given the ol' cfront mechanisms.

    So, what do you mean - *exactly*?
    pemo, Jan 4, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. New_aspect

    Aspect oriented Everything?

    New_aspect, Aug 22, 2003, in forum: Perl
    Replies:
    5
    Views:
    1,090
    Robert Will
    Aug 31, 2003
  2. =?Utf-8?B?RGF2aWQgV2hpdGNodXJjaC1CZW5uZXR0?=

    Newbie: Image button aspect ratio

    =?Utf-8?B?RGF2aWQgV2hpdGNodXJjaC1CZW5uZXR0?=, Nov 6, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    1,208
    Scott M.
    Nov 7, 2004
  3. Arthur Hsu
    Replies:
    5
    Views:
    5,911
    Steven Cheng[MSFT]
    Dec 8, 2004
  4. Julia Donawald
    Replies:
    0
    Views:
    493
    Julia Donawald
    Dec 30, 2004
  5. Chris Fogelklou
    Replies:
    36
    Views:
    1,371
    Chris Fogelklou
    Apr 20, 2004
Loading...

Share This Page