473,663 Members | 2,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(MyCla ss( oldObject )).

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

Jul 19 '05 #1
6 9469

"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(MyCla ss( oldObject )).

What makes you think this would do anything less than
push_back(oldOb ject);
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(MyCla ss( 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(oldOb ject);

?
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(MyCla ss( oldObject )).

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


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<cha r> 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
maintainablilit y issues.
Jul 19 '05 #7

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

Similar topics

42
5758
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
15
21186
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 pointers) and for objects types call their default constructor. Any others points i should know?
8
20021
by: Jesper | last post by:
Hi, Does the concept "copy constructor" from c++ excist in c#. What is the syntax. best regards Jesper.
10
2559
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, we do not need to use the copy-constructor. So depending on the above reasoning I can avoid call by value and return by value for class objects, this bypasses the problem or it seems to me like that. Could any one give me some simple examples...
8
4292
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
3608
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 v);
9
2887
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 Foo() is included, i believe. };
0
8436
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
8345
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,...
1
8548
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
8634
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
5657
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();...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
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
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
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...

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.