Hello everyone,
I want to create an array of objects at run-time.
AFAIU, operator new[] will call the default constructor for each object
in the array. In other words, the following program will print INSIDE
DEFAULT CTOR five times.
#include <vector>
#include <cstdio>
struct Foo
{
Foo(int u) {
puts("INSIDE CTOR"); p = new char[u];
}
Foo() {
puts("INSIDE DEFAULT CTOR"); p = new char[666];
}
~Foo() {
puts("INSIDE DTOR"); delete[] p;
}
char *p;
};
int main()
{
Foo *ww = new Foo[5];
return 0;
}
$ g++ -Wall -g3 vectest.cxx
vectest.cxx: In function `int main()':
vectest.cxx:20: warning: unused variable 'ww'
$ ./a.out
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
What if I want to use a different constructor?
For example, how can I get the program to create an array of 5 objects
that hold a 123-byte buffer?
Foo *ww = new Foo(123)[5];
is a syntax error. Am I missing something obvious?
I suppose I could add a static variable to class Foo and have the
default constructor use the value of that variable...
struct Foo
{
Foo(int u) {
puts("INSIDE CTOR"); p = new char[u];
}
Foo() {
puts("INSIDE DEFAULT CTOR"); p = new char[666];
}
~Foo() {
puts("INSIDE DTOR"); delete[] p;
}
char *p;
static int defaultsize;
};
int Foo::defaultsiz e = 0;
int main()
{
Foo::defaultsiz e = 123;
Foo *ww = new Foo[5];
return 0;
}
But that feels like a kludge. Is there a better solution?
On a related note, would a vector help in this situation?
I could write something along the lines of
std::vector < Foo v;
v.reserve(N);
for (int i=0; i < N; ++i)
{
Foo *curr = new Foo(size)
v.push_back(*cu rr);
}
But that feels somewhat like a kludge too.
Regards. 3 3207
Spoon wrote:
I want to create an array of objects at run-time.
AFAIU, operator new[] will call the default constructor for each
object in the array. In other words, the following program will print
INSIDE DEFAULT CTOR five times.
#include <vector>
#include <cstdio>
struct Foo
{
Foo(int u) {
puts("INSIDE CTOR"); p = new char[u];
}
Foo() {
puts("INSIDE DEFAULT CTOR"); p = new char[666];
}
~Foo() {
puts("INSIDE DTOR"); delete[] p;
}
char *p;
};
int main()
{
Foo *ww = new Foo[5];
return 0;
}
$ g++ -Wall -g3 vectest.cxx
vectest.cxx: In function `int main()':
vectest.cxx:20: warning: unused variable 'ww'
$ ./a.out
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
What if I want to use a different constructor?
You're SOL.
For example, how can I get the program to create an array of 5 objects
that hold a 123-byte buffer?
Foo *ww = new Foo(123)[5];
is a syntax error. Am I missing something obvious?
Not really. There is no way to do what you want in a single statement.
I suppose I could add a static variable to class Foo and have the
default constructor use the value of that variable...
struct Foo
{
Foo(int u) {
puts("INSIDE CTOR"); p = new char[u];
}
Foo() {
puts("INSIDE DEFAULT CTOR"); p = new char[666];
}
~Foo() {
puts("INSIDE DTOR"); delete[] p;
}
char *p;
static int defaultsize;
};
int Foo::defaultsiz e = 0;
int main()
{
Foo::defaultsiz e = 123;
Foo *ww = new Foo[5];
return 0;
}
But that feels like a kludge. Is there a better solution?
Use std::vector.
On a related note, would a vector help in this situation?
Yep.
I could write something along the lines of
std::vector < Foo v;
v.reserve(N);
for (int i=0; i < N; ++i)
{
Foo *curr = new Foo(size)
v.push_back(*cu rr);
}
But that feels somewhat like a kludge too.
Whatever you choose to call it. I call it "a work-around".
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On 19 Mar, 15:07, Spoon <devn...@localh ost.comwrote:
Hello everyone,
I want to create an array of objects at run-time.
AFAIU, operator new[] will call the default constructor for each object
in the array. In other words, the following program will print INSIDE
DEFAULT CTOR five times.
#include <vector>
#include <cstdio>
struct Foo
{
Foo(int u) {
puts("INSIDE CTOR"); p = new char[u];
}
Foo() {
puts("INSIDE DEFAULT CTOR"); p = new char[666];
}
~Foo() {
puts("INSIDE DTOR"); delete[] p;
}
char *p;
};
int main()
{
Foo *ww = new Foo[5];
return 0;
}
$ g++ -Wall -g3 vectest.cxx
vectest.cxx: In function `int main()':
vectest.cxx:20: warning: unused variable 'ww'
$ ./a.out
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
INSIDE DEFAULT CTOR
What if I want to use a different constructor?
For example, how can I get the program to create an array of 5 objects
that hold a 123-byte buffer?
Foo *ww = new Foo(123)[5];
is a syntax error. Am I missing something obvious?
I suppose I could add a static variable to class Foo and have the
default constructor use the value of that variable...
struct Foo
{
Foo(int u) {
puts("INSIDE CTOR"); p = new char[u];
}
Foo() {
puts("INSIDE DEFAULT CTOR"); p = new char[666];
}
~Foo() {
puts("INSIDE DTOR"); delete[] p;
}
char *p;
static int defaultsize;
};
int Foo::defaultsiz e = 0;
int main()
{
Foo::defaultsiz e = 123;
Foo *ww = new Foo[5];
return 0;
}
But that feels like a kludge. Is there a better solution?
On a related note, would a vector help in this situation?
I could write something along the lines of
std::vector < Foo v;
v.reserve(N);
for (int i=0; i < N; ++i)
{
Foo *curr = new Foo(size)
v.push_back(*cu rr);
}
std::vector<Foo v(5, Foo(4));
--
Erik Wikström
Erik Wikström wrote:
On 19 Mar, 15:07, Spoon <devn...@localh ost.comwrote:
>[..]
std::vector<Foo v(5, Foo(4));
... along with a proper implementation of the copy c-tor.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Nils |
last post by:
Hi,
I want to create a dynamic array with pointer, without allocation of the
memory.
I tried it so:
objekt **ob= new objekt;
It is not working, because he will parameter for the Konstructor ob objekt.
It works in the following way
|
by: Vasileios Zografos |
last post by:
Hello,
I have a function that generates some values (e.g. vertices in 2d space)
the number of which I dont know. So, it could generate 20 vertices, 100
vertices, or even 1 vertex.
void generateVals()
{
.....
//generate some values
|
by: Peter B. Steiger |
last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C
is a simple word game that involves stringing together random lists of
words. In the Pascal version the whole array was static; if the input
file contained more than entries, tough.
This time I want to do it right - use a dynamic array that increases in
size with each word read from the file. A few test programs that make use
of **List and realloc( List, blah...
|
by: lemonade |
last post by:
Hello!
Can someone explain to me the difference between dynamic
array of pointers vs dynamic array of objects by giving a
real life example. Following is the code that I am using
for dynamic array of objects. I am skipping initialization
and other member functions.
class Inventory {
Item *itemList;
int idx, count, size;
|
by: Jo |
last post by:
Hi,
How can i differentiate between static and dynamic allocated objects?
For example:
void SomeFunction1() {
CObject *objectp = new CObject;
CObject object;
| |
by: sk.rasheedfarhan |
last post by:
Hi ,
Here I am new user to C#, my problem is I have to use dynamic Array of
objects. But I heard C# don't support ptrs (using managed code C#
support).
In short i initialized objects of 1000 and I am using is upto 10 or
less. Because of that I find 990 reset of them as un initialized
objects, when I extract the information from the Object it will throw
an exception also and I feel unnecessarily I am wasting of memory.
So I need dynamic...
|
by: him1979 |
last post by:
If I have these classes:
class student {
private:
string * name;
int faculty number;
date datebirth;
public:
...
};
|
by: Steph |
last post by:
Hello,
I have filled a dynamic array of strings (realloc() + malloc())
char **sMyArray;
At the end, I get correctly sMyArray = "STRING_0", sMyArray =
"STRING_1", etc...
But I'm unable to use bsearch(). It doens't find any string.
|
by: David d'Angers |
last post by:
hi all
i need a function that returns all the file names contained in a
directory
since the number of files is not known in advance
how should i go about to use "dynamic array" to hold the file names ?
can anyone post some code snippets please?
thanks
|
by: leon70 |
last post by:
Hi Group,
I built a gSoap 2.7.11 Web services client in C++ on AIX and have got
it talking to the remote service. The service is sending back an
array of objects to my client but the client is unable to decode it.
The client seems to be having a problem with the "item" tag element
that is used to wrap the sequence of array objects.
The SOAP call returns a SOAP_OK - no exception or error returned to
the client. The returned XML...
|
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...
| |
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...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |