What implemetation for a copy on write pointer do you recommend that I use, since it's not a completely standard thing? I found the link to http://code.axter.com/cow_ptr.h, used it, and it crashed. I am compiling with visual studio v8.0 without a glitch and I am starting to think I just didn't understand how to use the pointer. Below, I attach a simple program that crashed, but works fine with a copy pointer from http://code.axter.com/copy_ptr.h. Thanks a lot in advance for your help
3 4527
Sorry and don't accuse me of flooding. I realized there was an error in my sample code -- I had been trying things out and I happen to send the wrong version (although it also crashes, even doing yet less than the one below). It should return
point[0] = ( 0 , 0 )
point[1] = ( 1 , 0 )
point[0] = ( 0 , 0 )
point[1] = ( 1 , 0 )
copypoint[0] = ( 49 , 0 )
copypoint[1] = ( 1 , 0 )
but the input is -
/* CLASS */
-
class point2D
-
{
-
protected:
-
double x_,y_;
-
public:
-
point2D() : x_(0), y_(0) {}
-
point2D(double x, double y) : x_(x), y_(y) {}
-
void setx(double x) { x_ = x;}
-
void sety(double y) { y_ = y;}
-
double getx() const { return x_;}
-
double gety() const { return y_;}
-
};
-
-
class point3D : public point2D
-
{
-
private:
-
double z_;
-
public:
-
point3D() : point2D(), z_(0) {}
-
point3D(double x, double y, double z) : point2D(x,y), z_(z) {}
-
void setz(double z) { z_ = z;}
-
double getz() const { return z_;}
-
};
-
and the main -
std::size_t npoints = 2;
-
std::vector<cow_ptr<point2D> > points(npoints);
-
-
// initialize
-
-
for(std::size_t i = 0; i < npoints; ++i)
-
{
-
points[i] = cow_ptr<point2D>(new point3D(i,0,i+1));
-
}
-
-
// first print to check input
-
-
for(std::size_t i = 0; i < points.size(); ++i)
-
{
-
std::cout << "point[" << i << "] = ( "
-
<< points[i]->getx() << " , "
-
<< points[i]->gety() << " )"
-
<< std::endl;
-
}
-
-
// copy vector and make changes
-
-
std::vector<cow_ptr<point2D> > copypoints = points;
-
copypoints[0]->setx(49.0);
-
-
// check that old vector and new vector are not shared
-
-
for(std::size_t i = 0; i < points.size(); ++i)
-
{
-
std::cout << "point[" << i << "] = ( "
-
<< points[i]->getx() << " , "
-
<< points[i]->gety() << " )"
-
<< std::endl;
-
}
-
for(std::size_t i = 0; i < copypoints.size(); ++i)
-
{
-
std::cout << "copypoint[" << i << "] = ( "
-
<< copypoints[i]->getx() << " , " <<
-
copypoints[i]->gety() << " )" <<
-
std::endl;
-
}
-
Sorry about that
You might read the article in the C/C++ HowTos about Handle Classes. That article contains a template for a smart pointer that does what you want.
Thanks a lot for the answer. I'll try it right away and let you know if it works. Regards
Sign in to post your reply or Sign up for a free account.
Similar topics
by: bluekite2000 |
last post by:
I want Matrix A(B) to create shallow copy of B but A=B to create deep
copy of B. Is that bad design? Why and why not?
|
by: simkn |
last post by:
Hello,
I'm writing a function that updates an array. That is, given an
array, change each element.
The trick is this: I can't change any elements until I've processed
the entire array. For...
|
by: baibaichen |
last post by:
i have written some code to verify how to disable slicing copy
according C++ Gotchas item 30
the follow is my class hierarchy, and note that B is abstract class!!
class B
{
public:
explicit...
|
by: PengYu.UT |
last post by:
The following program calls the normal constructor and the copy
constructor.
By calling the copy constuctor is redundandant, all I want is only a
vector of a trial object.
Is there any way to...
|
by: utab |
last post by:
Dear all,
How do experienced programmers using this group use the rule of
thumb,namely copy ctor, assignment operator, and dtor.
example
if the class does not need them dont use and define...
|
by: jan247 |
last post by:
Hi guys, thanks for taking time to read this..
Question 1
Let A be the superclass and B the subclass
A is not an abstract class.
print is a virtual function of A, which B implements.
A a =...
|
by: mosfet |
last post by:
Hi,
I read an article about COW implementation and issues in multithreaded
environments.
Then I found another article about counted reference and now I don't
understand anything. For instance...
|
by: Angus |
last post by:
I have a socket class CTestClientSocket which I am using to simulate
load testing.
I create multiple instances of the client like this:
for (int i = 0; i < 5; i++)
{
CTestClientSocket* pTemp...
|
by: Alex Snast |
last post by:
hello
I'm trying to implement a copy constructor for a vector of pointers to
a base class (which is abstract)
My question is how would i know what kind of new type should i
allocate since the...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
| |