How Can I init array m_obj in class MyContainer?

Discussion in 'C++' started by recover, Jan 18, 2007.

  1. recover

    recover Guest

    class Obj
    {
    public:
    Obj(int a){}
    }

    class MyContainer
    {
    private:
    Obj m_obj[10];
    public:
    MyContainer(){}
    }

    How Can I init array m_obj in class MyContainer? (not use std::vector).
    What I have to do can make these code pass complied?

    ----------------------------
    Co.:beijing lingtu Co.
    Ad.: beijing haidian
    ZIP£º 100094
    Tel.: 010-82825800£­8006
    Mobile:
    Mail£º
    MSN:
    comp£º http://www.lingtu.com/
    lingtu online:http://www.51ditu.com/
    --------------------------
    recover, Jan 18, 2007
    #1
    1. Advertising

  2. recover

    Ondra Holub Guest

    recover napsal:
    > class Obj
    > {
    > public:
    > Obj(int a){}
    > }
    >
    > class MyContainer
    > {
    > private:
    > Obj m_obj[10];
    > public:
    > MyContainer(){}
    > }
    >
    > How Can I init array m_obj in class MyContainer? (not use std::vector).
    > What I have to do can make these code pass complied?
    >
    > ----------------------------
    > Co.:beijing lingtu Co.
    > Ad.: beijing haidian
    > ZIP£º 100094
    > Tel.: 010-82825800£­8006
    > Mobile:
    > Mail£º
    > MSN:
    > comp£º http://www.lingtu.com/
    > lingtu online:http://www.51ditu.com/
    > --------------------------


    Items in array are initiated with default constructor. You can provide
    some function (let's say Create) which performs same initialization as
    would be in ordinary constructor. Or you can assign to every array item
    new instance created with your own non-default constructor. Or you can
    use std::vector.
    Ondra Holub, Jan 18, 2007
    #2
    1. Advertising

  3. recover

    Salt_Peter Guest

    recover wrote:
    > class Obj
    > {
    > public:
    > Obj(int a){}
    > }

    ;
    >
    > class MyContainer
    > {
    > private:
    > Obj m_obj[10];
    > public:
    > MyContainer(){}
    > }

    ;
    >
    > How Can I init array m_obj in class MyContainer? (not use std::vector).
    > What I have to do can make these code pass complied?


    Why not std::vector? It would simplify, enhance and empower your code.

    A primitive array invokes the default constructor of each element. So
    if you write class Obj properly, the elements of the array will be
    initialized by its def ctor.

    class Obj
    {
    int a;
    public:
    Obj() : a(0) { } // def ctor
    };

    template< typename T, const size_t Size >
    class Container
    {
    T m_array[Size];
    public:
    Container() { }
    };

    int main()
    {
    Container< Obj, 5 > container;
    }

    With a std::vector, the above container would be dynamic (resizeable)
    and you could use any ctor, not just a def ctor. In release mode, that
    vector is most likely to outperform the array as well.
    Salt_Peter, Jan 18, 2007
    #3
  4. recover

    Grizlyk Guest

    recover wrote:

    > How Can I init array m_obj in class MyContainer? (not use std::vector).
    > What I have to do can make these code pass complied?


    You need provide default ctor, and it seems to me, that it is not
    useful property of C++. It can be better, if one can init C-style array
    with any concrete ctor
    concrete_class ptr=new concrete_class(params)[size];
    or
    concrete_class ptr=new[size] concrete_class(params);
    Grizlyk, Jan 19, 2007
    #4
  5. recover

    recover Guest

    "recover" <> said
    > class Obj
    > {
    > public:
    > Obj(int a){}
    > }
    >
    > class MyContainer
    > {
    > private:
    > Obj m_obj[10];
    > public:
    > MyContainer(){}
    > }


    "Ondra Holub" <> said

    >>Items in array are initiated with default constructor.

    In some case,Obj must not have default constructor,or the default
    constructor has other use.

    >>You can provide some function (let's say Create) which performs same

    initialization as
    would be in ordinary constructor.
    this may be a good idea,if I can modify the class Obj. But if I dont't have
    the code of "class Obj",I import it from some lib or dll .Or the code of
    "clas Obj" is borrowed from others,I havn't right change these code.

    >>Or you can assign to every array item new instance created with your own

    non-default constructor.
    I cant't full understand this line. Can you give me some example?

    >>Or you can use std::vector.

    some complier doesn't support stl, may be in embeded equipment.So I can't
    use std::vector.

    Thanks a lot.
    recover, Jan 19, 2007
    #5
  6. recover

    recover Guest

    "Salt_Peter" <> wrote
    >
    > Why not std::vector? It would simplify, enhance and empower your code.
    >


    > With a std::vector, the above container would be dynamic (resizeable)
    > and you could use any ctor, not just a def ctor. In release mode, that
    > vector is most likely to outperform the array as well.
    >


    some complier doesn't support stl, may be in embeded equipment.So I can't
    use std::vector.
    recover, Jan 19, 2007
    #6
  7. recover

    recover Guest

    "Grizlyk" wrote
    > You need provide default ctor, and it seems to me, that it is not
    > useful property of C++. It can be better, if one can init C-style array
    > with any concrete ctor
    > concrete_class ptr=new concrete_class(params)[size];
    > or
    > concrete_class ptr=new[size] concrete_class(params);
    >


    Yes ,I like write like this.
    class Obj
    {
    public:
    Obj(int a){}
    }

    class MyContainer
    {
    private:
    //Init in here is better??Why c++ can't support this?
    Obj m_obj[3]={?Obj(3), Obj(7), Obj(4)?};
    public:
    MyContainer(){}
    }
    recover, Jan 19, 2007
    #7
  8. recover

    Grizlyk Guest

    recover wrote:
    > >
    > > You need provide default ctor, and it seems to me, that it is not
    > > useful property of C++. It can be better, if one can init C-style array
    > > with any concrete ctor
    > > concrete_class ptr=new concrete_class(params)[size];

    >
    > Obj m_obj[3]={?Obj(3), Obj(7), Obj(4)?};


    No, I was speaking about calling during "new[]" (after memory
    allocation has been done) for each member of array concrete ctor (equal
    for each member), instead of default ctor.
    Grizlyk, Jan 19, 2007
    #8
    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. Tony Cheng
    Replies:
    1
    Views:
    8,205
    Juan T. Llibre
    Feb 24, 2006
  2. Victor Hannak
    Replies:
    5
    Views:
    551
    Victor Hannak
    Sep 26, 2003
  3. Replies:
    1
    Views:
    656
    Jules
    Aug 18, 2005
  4. Jess
    Replies:
    4
    Views:
    438
  5. news.aon.at
    Replies:
    11
    Views:
    640
    Ian Collins
    Jan 29, 2011
Loading...

Share This Page