Declare a member variable of a class in the constructor

A

ankurdave

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.
 
J

Jonathan Mcdougall

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
 
R

Roy Smith

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.
 
I

Ian Collins

Roy said:
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.
 
B

benben

Ian said:
Roy said:
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 :)
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top