473,668 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constructor Question

I need to have a constructor that is passed a integer number that defines
the size of an array in the private area of the Class. Is that possible?

For example in main() the user is asked for the number of lanes. Then when
the object is declared after that it is passed that integer, which then
creates an array of pointers.
Jul 19 '05 #1
14 4347
- Steve - wrote:
I need to have a constructor that is passed a integer number that defines
the size of an array in the private area of the Class. Is that possible?

For example in main() the user is asked for the number of lanes. Then when
the object is declared after that it is passed that integer, which then
creates an array of pointers.


you could use a std::vector
i.e.

class Lanes
{

std::vector< Lane * > m_lane_array;

public:
Lanes( int size )
: m_lane_array( size )
{
}
};
Jul 19 '05 #2
TR
"- Steve -" <se****@foundat ion.sdsu.edu> wrote in message
news:k8******** *********@news2 .central.cox.ne t...
I need to have a constructor that is passed a integer number that defines
the size of an array in the private area of the Class. Is that possible?


Yes, constructors can take parameters like normal functions. Use an
std::vector in your private area and call .resize() in your constructor to
set the array size. In fact you don't even need to resize it, as it'll grow
as data is added. Get hold of an elementary C++ reference to learn more.
Jul 19 '05 #3
vectors are fine if you like std, personally i use std sparingly

all ya have to do is use new

Object *_array = new Objects[t];

or

typedef Object *PObject;
Object **_array = new PObject[t];
depending on what exactly your looking to do


"- Steve -" <se****@foundat ion.sdsu.edu> wrote in message
news:k8******** *********@news2 .central.cox.ne t...
I need to have a constructor that is passed a integer number that defines
the size of an array in the private area of the Class. Is that possible?

For example in main() the user is asked for the number of lanes. Then when the object is declared after that it is passed that integer, which then
creates an array of pointers.

Jul 19 '05 #4

"- Steve -" <se****@foundat ion.sdsu.edu> wrote in message
news:k8******** *********@news2 .central.cox.ne t...
I need to have a constructor that is passed a integer number that defines
the size of an array in the private area of the Class. Is that possible?

For example in main() the user is asked for the number of lanes. Then when the object is declared after that it is passed that integer, which then
creates an array of pointers.


You should declare your constructor to be explicit because the parameter
passed to the
constructor determines the object's configuration. This way you prevent any
implicit
conversions that may otherwise occur inadvertently.

With best wishes,
J.Schafer
Jul 19 '05 #5

"TR" <no@spam.com> wrote in message
news:R_******** *********@news-binary.blueyond er.co.uk...
"- Steve -" <se****@foundat ion.sdsu.edu> wrote in message
news:k8******** *********@news2 .central.cox.ne t...
I need to have a constructor that is passed a integer number that defines the size of an array in the private area of the Class. Is that
possible?
Yes, constructors can take parameters like normal functions. Use an
std::vector in your private area and call .resize() in your constructor to
set the array size. In fact you don't even need to resize it, as it'll grow as data is added. Get hold of an elementary C++ reference to learn more.


Err, you can't resize an array. I think you are thinking of a vector.

john
Jul 19 '05 #6

"- Steve -" <se****@foundat ion.sdsu.edu> wrote in message
news:k8******** *********@news2 .central.cox.ne t...
I need to have a constructor that is passed a integer number that defines
the size of an array in the private area of the Class. Is that possible?

For example in main() the user is asked for the number of lanes. Then when the object is declared after that it is passed that integer, which then
creates an array of pointers.


Don't use an array, use a vector. Something like this

#include <vector>

class LaneVector
{
public:
LaneVector(int num_lanes) : lanes(num_lanes )
{
}
private:
std::vector<Lan e> lanes;
};

You should really learn about vectors, make your life much easier.

john
Jul 19 '05 #7


dwrayment wrote:

vectors are fine if you like std, personally i use std sparingly

all ya have to do is use new

Object *_array = new Objects[t];


.... and by thus have opened the 'Hey why use something
simple when there is a more complicate way' season.

By doing the memory management by yourself, your class
suddenly also needs: a destructor, an assignment operator,
a copy constructor. And all of them have, of course, to be
written correctly!

So why would you want to do this, when vector is available
and saves you from all this possible pitfalls?

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 19 '05 #8
John Harrison wrote:

"TR" <no@spam.com> wrote in message
news:R_******** *********@news-binary.blueyond er.co.uk...
"- Steve -" <se****@foundat ion.sdsu.edu> wrote in message
news:k8******** *********@news2 .central.cox.ne t...
> I need to have a constructor that is passed a integer number that defines > the size of an array in the private area of the Class. Is that

possible?

Yes, constructors can take parameters like normal functions. Use an
std::vector in your private area and call .resize() in your constructor
to set the array size. In fact you don't even need to resize it, as it'll

grow
as data is added. Get hold of an elementary C++ reference to learn more.


Err, you can't resize an array. I think you are thinking of a vector.

john


That's why s/he said quite clearly "Use an STD::vector..." .

pete

Jul 19 '05 #9

"Peter Gregory" <pe***********@ durham.ac.uk> wrote in message
news:be******** **@sirius.dur.a c.uk...
John Harrison wrote:

"TR" <no@spam.com> wrote in message
news:R_******** *********@news-binary.blueyond er.co.uk...
"- Steve -" <se****@foundat ion.sdsu.edu> wrote in message
news:k8******** *********@news2 .central.cox.ne t...
> I need to have a constructor that is passed a integer number that

defines
> the size of an array in the private area of the Class. Is that

possible?

Yes, constructors can take parameters like normal functions. Use an
std::vector in your private area and call .resize() in your constructor
to set the array size. In fact you don't even need to resize it, as it'll
grow
as data is added. Get hold of an elementary C++ reference to learn

more.


Err, you can't resize an array. I think you are thinking of a vector.

john


That's why s/he said quite clearly "Use an STD::vector..." .

pete


Hmm, shortly followed by "call .resize() in your constructor to set the
array size", but maybe I should learn to read.

Also the statement "In fact you don't even need to resize it, as it'll grow
as data is added." is misleading at best.

john
Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4871
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but the class is used in a windows program and has an HWND member ( a handle to a window), and to make an sense the class must know what the member is. But this puts me in the position of having no default constructor, and I've heard that is...
24
3751
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
6
2697
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the memory allocation algorithm, but for gathering some statistics and to find memory leaks.) This seems to work. However, I noticed that my replacing delete operator is not called
45
6338
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
2
2189
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to instantiate an array of structures; consider the following useless code : using System; struct MyPointS
16
3745
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
74
15968
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
19
2383
by: Jeroen | last post by:
Hi guys, I have a simple question. If I have a class like: class A { A(); ~A(); A(A& a); A(int i);
1
1397
by: Bob Johnson | last post by:
Please note that this question is NOT about the merits of Microsoft certification (no need to get into that here). While taking a MS certification exam, I came across a question where it was obvious that two out of the five choices (a multiple choice question) would provide a workable solution to the scenario. The other chioces obviously incorrect. The *only* difference between the two alternatives is that they each used different...
4
987
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another copy which in itself needs a copy constructor. However i was wondering why can't the existing object be passed as a pointer instead of a reference to the copy constructor which creates a new object.
0
8459
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8374
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8575
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8653
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4202
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.