Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initilize array member's size in object constructor?

Reply
Thread Tools

initilize array member's size in object constructor?

 
 
crichmon
Guest
Posts: n/a
 
      07-04-2004
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


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-04-2004
* 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?
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      07-04-2004
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.


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      07-04-2004
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
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-04-2004
* 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?
 
Reply With Quote
 
Fraser Ross
Guest
Posts: n/a
 
      07-04-2004
A nontype template parameter can be used.

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

foo<30> test();

Fraser.


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-04-2004
"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>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
initilize a memory zone in python Mug Python 4 08-31-2009 08:08 AM
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
Howto isset Method? without initilize instance maborak@gmail.com Javascript 4 05-11-2007 07:28 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
Passing derived class object array in place of base class object array justanotherguy63@yahoo.com C++ 9 12-03-2004 10:57 PM



Advertisments