//
// Is there something wrong with my syntax for the
// Copy Constructor of an Array Element, or does
// the C++ language not support this?
//
#include <stdio.h>
#include <stdlib.h>
class X {
int Data;
public:
X() { Data = 56; };
X(const X& Y) { Data = Y.Data; }; // Copy Constructor
void Construct(const X& Y) { Data = Y.Data; };
};
int main()
{
X ABC;
X* Temp = (X*) malloc(sizeof(X ) * 10);
for (int N = 0; N < 10; N++) {
// Temp[N].(ABC); // Does Not Compile
// Temp[N](ABC); // Does Not Compile
Temp[N].Construct(ABC) ; // Compiles and Executes Correctly
}
free(Temp);
return 0;
} 26 2564
"Peter Olcott" <ol****@worldne t.att.net> wrote... // // Is there something wrong with my syntax for the // Copy Constructor of an Array Element, or does // the C++ language not support this?
The latter. Constructors do not have names, therefore they cannot
be found during name lookup and hence cannot be _called_. They can
only be _invoked_ during construction of the object.
Given this situation, you'd be better off using _placement_new_
(read about it in a good C++ book).
//
#include <stdio.h> #include <stdlib.h>
class X { int Data; public: X() { Data = 56; }; X(const X& Y) { Data = Y.Data; }; // Copy Constructor void Construct(const X& Y) { Data = Y.Data; }; };
int main() { X ABC; X* Temp = (X*) malloc(sizeof(X ) * 10); for (int N = 0; N < 10; N++) { // Temp[N].(ABC); // Does Not Compile // Temp[N](ABC); // Does Not Compile Temp[N].Construct(ABC) ; // Compiles and Executes Correctly } free(Temp); return 0; }
V
"Victor Bazarov" <v.********@com Acast.net> wrote in message: "Peter Olcott" <ol****@worldne t.att.net> wrote... // // Is there something wrong with my syntax for the // Copy Constructor of an Array Element, or does // the C++ language not support this?
The latter. Constructors do not have names, therefore they cannot be found during name lookup and hence cannot be _called_. They can only be _invoked_ during construction of the object.
Given this situation, you'd be better off using _placement_new_ (read about it in a good C++ book).
I'd recommend using an assignment operator or a member function assign
(like std library containers) instead of placement new, unless you are
absolutely sure you need it.
And if you do use placement new to constuct an object on top of an
existing object, make sure to call the objects destructor first
(unless you are absolutely sure it's not necessary.)
Jonathan
Peter Olcott wrote: // // Is there something wrong with my syntax for the // Copy Constructor of an Array Element, or does // the C++ language not support this? //
#include <stdio.h> #include <stdlib.h>
class X {
private:
// representation int Data; public: X(void): Data(56) { } // default X(const X& Y): Data(Y.Data) { } // Copy Constructor // void Construct(const X& Y) { Data = Y.Data; };
// too late. *this already constructed
X& modify(const X& Y) { Data = Y.Data; return *this; } };
int main(int argc, char* argv[]) { X ABC; X* Temp = (X*)malloc(size of(X)*10); // you shouldn't do this for (int N = 0; N < 10; ++N) {
Temp[N] = ABC; // uses default assignment
Temp[N].modify(ABC); } free(Temp); return 0; }
> I'd recommend using an assignment operator or a member function assign (like std library containers) instead of placement new, unless you are absolutely sure you need it.
I just created a std::vector class for antique (pre-template) C++ compilers.
My intention was to implement this within minimal execution time.
Apparently, it seems that even the modern std::vectors must use a kludge
to initialize their members. They seem to require default construction and
then assignment, rather than the single integrated step of copy construction.
Hopefully this shortcoming will by addressed in the continuing evolution
of C++.
And if you do use placement new to constuct an object on top of an existing object, make sure to call the objects destructor first (unless you are absolutely sure it's not necessary.)
Jonathan
"Peter Olcott" <ol****@worldne t.att.net> wrote in message
news:ZP******** ************@bg tnsc05-news.ops.worldn et.att.net... I'd recommend using an assignment operator or a member function assign (like std library containers) instead of placement new, unless you are absolutely sure you need it. I just created a std::vector class for antique (pre-template) C++
compilers. My intention was to implement this within minimal execution time. Apparently, it seems that even the modern std::vectors must use a kludge to initialize their members. They seem to require default construction and then assignment, rather than the single integrated step of copy
construction.
[snip]
Well, not really:
std::vector<int > a;
a.reserve(3);
a.push_back(2);
a.push_back(4);
a.push_back(-2);
reserve just allocates enough space and a copy constructor is used for each
element. No default constructor and no assignment. The only inefficiency
that I know of is that the number of push_back's is counted.
> > class X { private: // representation int Data; public: X(void): Data(56) { } // default X(const X& Y): Data(Y.Data) { } // Copy Constructor // void Construct(const X& Y) { Data = Y.Data; }; // too late. *this already constructed
I don't think so. I think that the example might be the only
way to copy construct array elements.
X& modify(const X& Y) { Data = Y.Data; return *this; } };
int main(int argc, char* argv[]) { X ABC; X* Temp = (X*)malloc(size of(X)*10); // you shouldn't do this for (int N = 0; N < 10; ++N) { Temp[N] = ABC; // uses default assignment Temp[N].modify(ABC); } free(Temp); return 0; }
> std::vector<int > a; a.reserve(3); a.push_back(2); a.push_back(4); a.push_back(-2);
reserve just allocates enough space and a copy constructor is used for each element. No default constructor and no assignment. The only inefficiency that I know of is that the number of push_back's is counted.
Try the same sort of example with a class that does memory allocation,
such that a deep copy (rather than bitwise) is required.
"Peter Olcott" <ol****@worldne t.att.net> wrote in message
news:nf******** ************@bg tnsc05-news.ops.worldn et.att.net... std::vector<int > a; a.reserve(3); a.push_back(2); a.push_back(4); a.push_back(-2);
reserve just allocates enough space and a copy constructor is used
for each element. No default constructor and no assignment. The only
inefficiency that I know of is that the number of push_back's is counted. Try the same sort of example with a class that does memory
allocation, such that a deep copy (rather than bitwise) is required.
Cy is correct.A vector obtains unitialized memory from an allocator,
and initilizes each element, when appropriate, then initializes them
using copy constructors with placement new. A default constructor is
not even required to exist. (See 20.1.4).
Of course, if the copy constructor is expensive, then push_back will
be too. There's no way to avoid this.
Jonathan
"Jonathan Turkanis" <te******@kanga roologic.com> wrote in message
news:c1******** *****@ID-216073.news.uni-berlin.de... Cy is correct.A vector obtains unitialized memory from an allocator, and initilizes each element, when appropriate, then initializes them using copy constructors with placement new. A default constructor is
This came out garbled. It should say:
A vector obtains unitialized memory from an allocator, then initilizes
each element, when appropriate, using copy constructors with placement
new
Jonathan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: franky.backeljauw |
last post by:
Hello,
I am wondering which of these two methods is the fastest: std::copy, which
is included in the standard library, or a manually written pointer copy?
Do any of you have any experience with this?
I would think that the library function std::copy would perform optimally,
as it is a library function, and therefore the writers of this function
would know best how to optimize it ... but some tests seem to indicate
that my pointer copy...
|
by: dmoos AT esigma-systems DOT de |
last post by:
Hi all,
i've got a simple question: how does the default copy-constructor act
on member-arrays ? Since array-variables are often treated as pointers
to the array-element-type, one could think that the default copy-
constructor would just copy the address of the first element of the
member-array-variables.
A simple test-programm reveals, that g++-2.96 instead copies the
member-array array-element by array-element.
|
by: Gary |
last post by:
Hi,
I am a Chinese student, I have a problem with the following code
//The follwing code in StaticSearch.h:
//
template <class Type> class dataList;
//
template <class Type> class Node //Êý¾Ý±íÖнáµãÀàµÄ¶¨Òå
|
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
|
by: pragtideep |
last post by:
Kindly help me explain the behaviour of defult copy constructor . Why
the destructor is freeing the SAME memory twice , though it was
allocated just once .
#include<iostream>
using namespace std;
class var_array
{
private:
int *data; // The data
| |
by: wij |
last post by:
Hi:
I don't understand why the following program doesn't compile.
Can anyone explain?
/* Build: g++ t.cpp */
#include <iostream>
class Foo {
Foo(const Foo& rhs) { std::cout << "Foo(const Foo&) "; };
public:
|
by: Jeremy Smith |
last post by:
I am writing a program where speed is crucial, and one optimisation is
that blocks of memory are re-used instead of being copied around and re-
allocate.
I have written my own vector-style class to achieve this. Here is what
happens to the reference count:
class MyArray:
Constructor:
|
by: rKrishna |
last post by:
I was trying to understand the real need for copy constructors. From
literature, the main reason for redfinition of copy constructor in a
program is to allow deep copying; meaning ability to make copies of
classes with members with static or dynamic memory allocation. However
to do this it requires the programmer to override the default copy
constructor, the destructor & operator=.
I wrote a small program to test if i can doa deep copy...
|
by: Peter Olcott |
last post by:
Why can a union have a member with a copy constructor?
|
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: 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,...
| |
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: 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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |