Bult-in array of objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • X.

    Bult-in array of objects

    Hi,

    There's been a discussion here on a similar topic that I'd like to
    push a step further.
    Suppose I get a class S having constructors:
    S(int n) - allocates n bytes of memory
    S() - allocates nothing
    and a copy constructor,cop y assigment op. and so on.

    I intend to declare a table of structures that contain (among other
    things)objects of S with some memory allocated. When I simple-minded
    code something like this:
    struct {
    S field1(20); //error here
    ...
    } x[100];

    then my compiler insists to use default constructor which doesn't
    allocate anything. And all my attempts to apply some object oriented
    programming like:
    class X {
    S field1;
    ...
    X(int n): field1(n) {}
    };

    X x[100];
    for(i...)
    { X xxx(20); //to get space for xxx
    x[i] = xxx; //hope the memory chunk will be assigned to x[i]
    }
    fall short because the operator= doesn't allocate memory, and flags an
    error. In other words, it can't copy an object to an "empty" one).

    Well, I could use vector<> or another container (or even overload
    operators of S yet I'd prefer not doing so) but this thought's
    torturing me in this New Year Eve: how to use a built-in tables to
    keep objects? Books (ex.Lippmann's one) claim that's possible but give
    no hints.

    Happy New Year to those who will reply to my post and those who won't.
    X.
  • Jonathan Mcdougall

    #2
    Re: Bult-in array of objects

    X. wrote:[color=blue]
    > Hi,
    >
    > There's been a discussion here on a similar topic that I'd like to
    > push a step further.
    > Suppose I get a class S having constructors:
    > S(int n) - allocates n bytes of memory
    > S() - allocates nothing
    > and a copy constructor,cop y assigment op. and so on.
    >
    > I intend to declare a table of structures that contain (among other
    > things)objects of S with some memory allocated. When I simple-minded
    > code something like this:[/color]

    Arrays can only be default-constructed. Either
    use standard containers (or roll you own) or use
    an array of pointers.


    Jonathan

    Comment

    • Jeff Flinn

      #3
      Re: Bult-in array of objects

      X. wrote:[color=blue]
      > Hi,[/color]

      ....
      [color=blue]
      > Well, I could use vector<> or another container (or even overload
      > operators of S yet I'd prefer not doing so) but this thought's
      > torturing me in this New Year Eve: how to use a built-in tables to
      > keep objects? Books (ex.Lippmann's one) claim that's possible but give
      > no hints.[/color]

      See boost.array and boost.assignmen t libraries at www.boost.org.

      [color=blue]
      > Happy New Year to those who will reply to my post and those who won't.[/color]

      Ditto.

      Jeff


      Comment

      • adbarnet

        #4
        Re: Bult-in array of objects

        With a glass or two of New Year's fortitude already down, I'd say this looks
        like a job for placement new.

        Shoot me where I sit if that's completely off the mark. I'll die happy.

        "X." <steamroller192 2@yahoo.com> wrote in message
        news:285664b5.0 412310710.69725 2d4@posting.goo gle.com...[color=blue]
        > Hi,
        >
        > There's been a discussion here on a similar topic that I'd like to
        > push a step further.
        > Suppose I get a class S having constructors:
        > S(int n) - allocates n bytes of memory
        > S() - allocates nothing
        > and a copy constructor,cop y assigment op. and so on.
        >
        > I intend to declare a table of structures that contain (among other
        > things)objects of S with some memory allocated. When I simple-minded
        > code something like this:
        > struct {
        > S field1(20); //error here
        > ...
        > } x[100];
        >
        > then my compiler insists to use default constructor which doesn't
        > allocate anything. And all my attempts to apply some object oriented
        > programming like:
        > class X {
        > S field1;
        > ...
        > X(int n): field1(n) {}
        > };
        >
        > X x[100];
        > for(i...)
        > { X xxx(20); //to get space for xxx
        > x[i] = xxx; //hope the memory chunk will be assigned to x[i]
        > }
        > fall short because the operator= doesn't allocate memory, and flags an
        > error. In other words, it can't copy an object to an "empty" one).
        >
        > Well, I could use vector<> or another container (or even overload
        > operators of S yet I'd prefer not doing so) but this thought's
        > torturing me in this New Year Eve: how to use a built-in tables to
        > keep objects? Books (ex.Lippmann's one) claim that's possible but give
        > no hints.
        >
        > Happy New Year to those who will reply to my post and those who won't.
        > X.
        >[/color]


        Comment

        Working...