initilize array member's size in object constructor?

Discussion in 'C++' started by crichmon, Jul 4, 2004.

  1. crichmon

    crichmon Guest

    Is there away to initialize an array's size through constructor
    initialization?

    Right now I have

    ///////////////
    class foo
    {
    string bar[20];
    public:
    foo();
    }

    ////
    foo::foo()
    {
    };


    ////////////////

    could something be done like

    ///////////////
    class foo
    {
    string bar[];
    public:
    foo();
    }

    ////
    foo::foo(): bar[20]
    {
    };


    ////////////////

    Any help?

    thanks,
    crichmon
     
    crichmon, Jul 4, 2004
    #1
    1. Advertising

  2. * crichmon:
    >
    > could something be done like
    >
    > ///////////////
    > class foo
    > {
    > string bar[];
    > public:
    > foo();
    > }
    >
    > ////
    > foo::foo(): bar[20]
    > {
    > };


    class Foo
    {
    private:
    std::vector<std::string> bar;
    public:
    Foo(): bar(20) {}
    };

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jul 4, 2004
    #2
    1. Advertising

  3. crichmon

    Rolf Magnus Guest

    crichmon wrote:

    > Is there away to initialize an array's size through constructor
    > initialization?
    >
    > Right now I have
    >
    > ///////////////
    > class foo
    > {
    > string bar[20];
    > public:
    > foo();
    > }
    >
    > ////
    > foo::foo()
    > {
    > };


    class foo
    {
    string* bar;
    public:
    foo();
    //don't forget copy construction/assignment/destructor
    };

    foo::foo()
    : bar(new string[20])
    {
    }

    > ////////////////
    >
    > could something be done like
    >
    > ///////////////
    > class foo
    > {
    > string bar[];
    > public:
    > foo();
    > }
    >
    > ////
    > foo::foo(): bar[20]
    > {
    > };


    No. Why would you want that? Note that a class has a size that is fixed
    at compile time, so you couldn't put a non-constant expression between
    the brackets anyway. And in the other case, you wouldn't gain anything.
     
    Rolf Magnus, Jul 4, 2004
    #3
  4. On Sun, 04 Jul 2004 08:42:06 GMT, crichmon <> wrote:

    > Is there away to initialize an array's size through constructor
    > initialization?
    >
    > Right now I have
    >
    > ///////////////
    > class foo
    > {
    > string bar[20];
    > public:
    > foo();
    > }
    >
    > ////
    > foo::foo()
    > {
    > };
    >
    >
    > ////////////////
    >
    > could something be done like
    >
    > ///////////////
    > class foo
    > {
    > string bar[];
    > public:
    > foo();
    > }
    >
    > ////
    > foo::foo(): bar[20]
    > {
    > };
    >
    >
    > ////////////////
    >
    > Any help?
    >
    > thanks,
    > crichmon
    >
    >


    Use a vector

    class foo
    {
    vector<string> bar;
    public:
    foo();
    }

    ////
    foo::foo(): bar(20)
    {
    };

    It's what vectors are for.

    john
     
    John Harrison, Jul 4, 2004
    #4
  5. * John Harrison:
    >
    > Use a vector
    >
    > class foo
    > {
    > vector<string> bar;
    > public:
    > foo();
    > }


    Here should be a semicolon.


    > ////
    > foo::foo(): bar(20)
    > {
    > };


    Here should not be a semicolon.

    I gather my reply to the same posting had not yet reached your
    news-server.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Jul 4, 2004
    #5
  6. crichmon

    Fraser Ross Guest

    A nontype template parameter can be used.

    template <int val=20>
    class foo
    {
    string bar[val];
    };

    foo<30> test();

    Fraser.
     
    Fraser Ross, Jul 4, 2004
    #6
  7. crichmon

    Rolf Magnus Guest

    "Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote:

    > A nontype template parameter can be used.
    >
    > template <int val=20>
    > class foo
    > {
    > string bar[val];
    > };
    >
    > foo<30> test();


    <Nitpick>
    Note that 'test' is a declaration of a function that returns a foo<30>.
    I don't think you actually wanted that.
    </Nitpick>
     
    Rolf Magnus, Jul 4, 2004
    #7
    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. Generic Usenet Account
    Replies:
    10
    Views:
    2,245
  2. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,586
    Michael Jung
    May 25, 2008
  3. Mug
    Replies:
    4
    Views:
    926
    Diez B. Roggisch
    Aug 31, 2009
  4. Bubba
    Replies:
    2
    Views:
    400
    Bubba
    Mar 9, 2012
  5. Replies:
    4
    Views:
    97
    -Lost
    May 11, 2007
Loading...

Share This Page