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

copy constructor

I am using push_back to place a class object into a vector. This class
contains data members that are pointers to a char string. When I use
push_back, I believe a copy of the object is first made, and then deposited
into the vector.

But I want this copy to not mereley copy the pointer to the char string, but
rather, I want to malloc new space, and make a copy of the char string.

The default copy constructor is defined as

MyClass( const MyClass& oldObject ).

At this stage, my method that defines the copy constructor would copy the
char string from oldObject to the new object. With push_back I can't do
this. The only way I can think of to do this, is to make two copies of the
object, one for the purpose of using the above copy constructor, and a
second when it is push_back'd to the vector.

This can be rather expensive. Is there a way I can achieve what I want and
avoid doing duplicat work?

-charles
I might be able to use the following syntax:

push_back(MyClass( oldObject )).

Will this work, and will it avoid making two copies?

Jul 19 '05 #1
6 9393

"Charles Herman" <sp**@no.spam> wrote in message news:3f********@127.0.0.1...
But I want this copy to not mereley copy the pointer to the char string, but
rather, I want to malloc new space, and make a copy of the char string.

The default copy constructor is defined as

MyClass( const MyClass& oldObject ). \
First off, do NOT confuse char* with a string. A char* is a pointer to a single
character. Perhaps the easiest way around this is to NOT use char*. This
is C++, use std::string. std::string's constructors, assignment operators, and
destructors handle all this memory management for you.
push_back(MyClass( oldObject )).

What makes you think this would do anything less than
push_back(oldObject);
Jul 19 '05 #2
Charles Herman wrote:
I am using push_back to place a class object into a vector. This class
contains data members that are pointers to a char string. When I use
push_back, I believe a copy of the object is first made, and then
deposited into the vector.

But I want this copy to not mereley copy the pointer to the char
string, but rather, I want to malloc new space, and make a copy of the
char string.

The default copy constructor is defined as

MyClass( const MyClass& oldObject ).

At this stage, my method that defines the copy constructor would copy
the char string from oldObject to the new object. With push_back I
can't do this.
Why?
The only way I can think of to do this, is to make two copies of the
object, one for the purpose of using the above copy constructor, and a
second when it is push_back'd to the vector.
Why do you want to copy the object at all before push_back'ing it into
the vector?
This can be rather expensive. Is there a way I can achieve what I want
and avoid doing duplicat work?
I don't understand your question. If you don't want to do an additional
copy, simply don't do it.
I might be able to use the following syntax:

push_back(MyClass( oldObject )).

Will this work, and will it avoid making two copies?


On the contrary, it adds a copy. What makes you believe that this does
one copy less than a simple:

push_back(oldObject);

?
Jul 19 '05 #3
Ron Natalie wrote:

"Charles Herman" <sp**@no.spam> wrote in message
news:3f********@127.0.0.1...
But I want this copy to not mereley copy the pointer to the char string,
but rather, I want to malloc new space, and make a copy of the char
string.

The default copy constructor is defined as

MyClass( const MyClass& oldObject ). \


First off, do NOT confuse char* with a string. A char* is a pointer to a
single
character. Perhaps the easiest way around this is to NOT use char*.
This
is C++, use std::string. std::string's constructors, assignment
operators, and destructors handle all this memory management for you.

push_back(MyClass( oldObject )).

What makes you think this would do anything less than
push_back(oldObject);


The problem with std::string, is that it is too slow; accesing an element of
a string takes anywhere from 2 to 4 times as long as acccessing an element
from a char* (this depeneds on which compiler one is using, I'm using g++
on Solaris, and it takes twice as long).

Also sometimes I want to make a copy of the string, and other times I want
to merely point to the original, when creating a copy of the object. In
either case, when I use char*, I have a pointer.

This is the reason for this question?

-charles

Jul 19 '05 #4
Charles Herman wrote:


The problem with std::string, is that it is too slow; accesing an element of
a string takes anywhere from 2 to 4 times as long as acccessing an element
from a char* (this depeneds on which compiler one is using, I'm using g++
on Solaris, and it takes twice as long).
So how many strings does your application need to process
per second and is the bottleneck really string processing?
Also sometimes I want to make a copy of the string, and other times I want
to merely point to the original, when creating a copy of the object. In
either case, when I use char*, I have a pointer.


So use a pointer to a string.

Jul 19 '05 #5
"Charles Herman" <sp**@no.spam> wrote in message
news:3f********@127.0.0.1...
The problem with std::string, is that it is too slow; accesing an element of a string takes anywhere from 2 to 4 times as long as acccessing an element
from a char* (this depeneds on which compiler one is using, I'm using g++
on Solaris, and it takes twice as long).


Then use std::vector<char> instead of char*. std::vector is copyable and
assignable without you having to manage the memory. Also, the vector's
storage is contiguous, making it viable as a replacement for an array.

Paul


Jul 19 '05 #6

"Charles Herman" <sp**@no.spam> wrote in message news:3f********@127.0.0.1...
The problem with std::string, is that it is too slow; accesing an element of a string takes anywhere from 2 to 4 times as long as acccessing an element
from a char* (this depeneds on which compiler one is using, I'm using g++
on Solaris, and it takes twice as long).


I just tested G++ on solaris and you are wrong. The difference in time is
negligable. Are you sure you are using an optimization setting that supports
inlining? Most of the gripes about the standard library performance come from
people who bench mark the things with inlining supressed so that the overloaded
operators incur function call overhead.
Also sometimes I want to make a copy of the string, and other times I want
to merely point to the original, when creating a copy of the object. In
either case, when I use char*, I have a pointer.


Yes, but you get messed up if you don't manage making the copy in contexts
where you need a copy. And once you go to the effort of managing the copy
you've spent as much effort as switching to vector or string, and you cause
maintainablility issues.
Jul 19 '05 #7

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
8
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
10
by: utab | last post by:
Dear all, So passing and returning a class object is the time when to include the definition of the copy constructor into the class definition. But if we don't call by value or return by value, ...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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.