By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,272 Members | 1,272 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,272 IT Pros & Developers. It's quick & easy.

Declare a member variable of a class in the constructor

P: n/a
Is it possible to declare a class member variable in the constructor?
For example,

class SomeClass
{
public:
SomeClass()
{ int SomeArray[10]; }
}

In this example, is it possible to make SomeArray[] a member variable
without putting the declaration like this:

class SomeClass
{
public:
SomeClass();
private:
int SomeArray[10];
}

The reason I am asking this is that I would like to create an array
whose size is determined by the parameters passed into the constructor.

May 13 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ankurd...@gmail.com wrote:
Is it possible to declare a class member variable in the constructor?
For example,

class SomeClass
{
public:
SomeClass()
{ int SomeArray[10]; }
}

In this example, is it possible to make SomeArray[] a member variable
without putting the declaration like this:

class SomeClass
{
public:
SomeClass();
private:
int SomeArray[10];
}
No, that's impossible.
The reason I am asking this is that I would like to create an array
whose size is determined by the parameters passed into the constructor.


Either

1) dynamically allocate it:

class SomeClass
{
public:
SomeClass(const std::size_t size)
{
SomeArray = new int[size];
}

~SomeClass()
{
delete[] SomeArray;
}

private:
int* SomeArray;
};

and do read about the "rule of three" (see especially chapter 16 of the
FAQ).

or

2) use a standard container

# include <vector>

class SomeClass
{
public:
SomeClass(const std::size_t size)
: v(size)
{
}

private:
std::vector<int> v;
};

Note that in particular a) standard containers manage their own memory
and b) they can grow as needed so the "size" argument is not necessary
(except for performance).

comp.lang.c++ FAQ: http://www.parashift.com/c++-faq-lite/
Jonathan

May 13 '06 #2

P: n/a
In article <11**********************@d71g2000cwd.googlegroups .com>,
an*******@gmail.com wrote:
Is it possible to declare a class member variable in the constructor?
For example,

class SomeClass
{
public:
SomeClass()
{ int SomeArray[10]; }
}
[...]
The reason I am asking this is that I would like to create an array
whose size is determined by the parameters passed into the constructor.


Have your constructor malloc() enough space for the array and store a
pointer to it (and its size) as member variables. Don't forget to free it
in the destructor.

SomeClass (int count) {
SomeArray = (int *) malloc (count * sizeof(int));
ArraySize = count;
}

~SomeClass () {
free (SomeArray);
}

Of course, in the sample above I've left out any error checking, which
you'll want to remedy in your production code. You could use new instead
of malloc() if you prefer.

Figuring out how your default constructor should behave is left as an
exercize for the reader.
May 13 '06 #3

P: n/a
Roy Smith wrote:
In article <11**********************@d71g2000cwd.googlegroups .com>,
an*******@gmail.com wrote:
Is it possible to declare a class member variable in the constructor?
For example,

class SomeClass
{
public:
SomeClass()
{ int SomeArray[10]; }
}
[...]
The reason I am asking this is that I would like to create an array
whose size is determined by the parameters passed into the constructor.

Have your constructor malloc() enough space for the array and store a
pointer to it (and its size) as member variables. Don't forget to free it
in the destructor.

Why malloc? In C++, new/delete is preferred.

--
Ian Collins.
May 13 '06 #4

P: n/a
Ian Collins wrote:
Roy Smith wrote:
In article <11**********************@d71g2000cwd.googlegroups .com>,
an*******@gmail.com wrote:
Is it possible to declare a class member variable in the constructor?
For example,

class SomeClass
{
public:
SomeClass()
{ int SomeArray[10]; }
}
[...]
The reason I am asking this is that I would like to create an array
whose size is determined by the parameters passed into the constructor.


Have your constructor malloc() enough space for the array and store a
pointer to it (and its size) as member variables. Don't forget to free it
in the destructor.

Why malloc? In C++, new/delete is preferred.


In this case, std::vector is preferred :)
May 14 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.