How Can I init array m_obj in class MyContainer?

R

recover

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£º[email protected]
MSN: (e-mail address removed)
comp£º http://www.lingtu.com/
lingtu online:http://www.51ditu.com/
--------------------------
 
O

Ondra Holub

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£º[email protected]
MSN: (e-mail address removed)
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.
 
S

Salt_Peter

recover said:
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.
 
G

Grizlyk

recover said:
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);
 
R

recover

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

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

In some case,Obj must not have default constructor,or the default
constructor has other use.
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.
non-default constructor.
I cant't full understand this line. Can you give me some example?
some complier doesn't support stl, may be in embeded equipment.So I can't
use std::vector.

Thanks a lot.
 
R

recover

Salt_Peter said:
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.
 
R

recover

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(){}
}
 
G

Grizlyk

recover said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top