473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Declare a member variable of a class in the constructor

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
4 2813
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
7
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this...
5
by: meyousikmann | last post by:
Given these two (incomplete but representative) classes in two seperate header files: Class1.h class Class1 { public: Class(const char CharValue, const int IntValue1, const int IntValue2);...
6
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
8
by: Tim Clacy | last post by:
1) Is this initialising the reference 'u' to the address of the literal '2' or to the address 0x00000002? unsigned const& u = 2; 2) What is the different between the initialisation of 'u'...
4
by: Henrik Goldman | last post by:
Hi, Lets say I have a class a which looks like the following: class a { public: a(int i) { m_nI = i;
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.