473,382 Members | 1,786 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,382 software developers and data experts.

Bult-in array of objects

X.
Hi,

There's been a discussion here on a similar topic that I'd like to
push a step further.
Suppose I get a class S having constructors:
S(int n) - allocates n bytes of memory
S() - allocates nothing
and a copy constructor,copy assigment op. and so on.

I intend to declare a table of structures that contain (among other
things)objects of S with some memory allocated. When I simple-minded
code something like this:
struct {
S field1(20); //error here
...
} x[100];

then my compiler insists to use default constructor which doesn't
allocate anything. And all my attempts to apply some object oriented
programming like:
class X {
S field1;
...
X(int n): field1(n) {}
};

X x[100];
for(i...)
{ X xxx(20); //to get space for xxx
x[i] = xxx; //hope the memory chunk will be assigned to x[i]
}
fall short because the operator= doesn't allocate memory, and flags an
error. In other words, it can't copy an object to an "empty" one).

Well, I could use vector<> or another container (or even overload
operators of S yet I'd prefer not doing so) but this thought's
torturing me in this New Year Eve: how to use a built-in tables to
keep objects? Books (ex.Lippmann's one) claim that's possible but give
no hints.

Happy New Year to those who will reply to my post and those who won't.
X.
Jul 22 '05 #1
3 1079
X. wrote:
Hi,

There's been a discussion here on a similar topic that I'd like to
push a step further.
Suppose I get a class S having constructors:
S(int n) - allocates n bytes of memory
S() - allocates nothing
and a copy constructor,copy assigment op. and so on.

I intend to declare a table of structures that contain (among other
things)objects of S with some memory allocated. When I simple-minded
code something like this:


Arrays can only be default-constructed. Either
use standard containers (or roll you own) or use
an array of pointers.
Jonathan
Jul 22 '05 #2
X. wrote:
Hi,
....
Well, I could use vector<> or another container (or even overload
operators of S yet I'd prefer not doing so) but this thought's
torturing me in this New Year Eve: how to use a built-in tables to
keep objects? Books (ex.Lippmann's one) claim that's possible but give
no hints.
See boost.array and boost.assignment libraries at www.boost.org.

Happy New Year to those who will reply to my post and those who won't.


Ditto.

Jeff
Jul 22 '05 #3
With a glass or two of New Year's fortitude already down, I'd say this looks
like a job for placement new.

Shoot me where I sit if that's completely off the mark. I'll die happy.

"X." <st*************@yahoo.com> wrote in message
news:28**************************@posting.google.c om...
Hi,

There's been a discussion here on a similar topic that I'd like to
push a step further.
Suppose I get a class S having constructors:
S(int n) - allocates n bytes of memory
S() - allocates nothing
and a copy constructor,copy assigment op. and so on.

I intend to declare a table of structures that contain (among other
things)objects of S with some memory allocated. When I simple-minded
code something like this:
struct {
S field1(20); //error here
...
} x[100];

then my compiler insists to use default constructor which doesn't
allocate anything. And all my attempts to apply some object oriented
programming like:
class X {
S field1;
...
X(int n): field1(n) {}
};

X x[100];
for(i...)
{ X xxx(20); //to get space for xxx
x[i] = xxx; //hope the memory chunk will be assigned to x[i]
}
fall short because the operator= doesn't allocate memory, and flags an
error. In other words, it can't copy an object to an "empty" one).

Well, I could use vector<> or another container (or even overload
operators of S yet I'd prefer not doing so) but this thought's
torturing me in this New Year Eve: how to use a built-in tables to
keep objects? Books (ex.Lippmann's one) claim that's possible but give
no hints.

Happy New Year to those who will reply to my post and those who won't.
X.

Jul 22 '05 #4

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

Similar topics

1
by: venkat gunreddy | last post by:
I am trying to import a text file with an Access 2000 front end application and The application has SQL SERVER 2000 as the back end. when i try to import the text file I am getting the error...
1
ok
by: moiseau | last post by:
hi looking for someone to bult my web site for me
2
by: Gilles Ganault | last post by:
Hello I need to access SQLite from PHP5 without recompiling PHP. Is there a way to just drop the DLL (or rather, the .so file : It's Linux) and use a special PHP function to use it? Thank you.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.