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? 6 9496
"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);
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);
?
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
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.
"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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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?
|
by: Jesper |
last post by:
Hi,
Does the concept "copy constructor" from c++ excist in
c#. What is the syntax.
best regards Jesper.
|
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...
|
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)
{}
| |
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);
|
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.
};
|
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: 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: 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.
|
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...
| |