J
Jeroen
Hi all,
I'm trying to implement a certain class but I have problems regarding
the copy ctor. I'll try to explain this as good as possible and show
what I tried thusfar. Because it's not about a certain code syntax but
more a 'code architecture' thing , I'll use simple example classes
(which are certainly not complete or working...) just to illustrate the
idea (and I may make some mistakes because I'm not that experienced...).
The basic idea is that I have a class which has a data vector, but
objects of that class can also share that data vector between several
instances:
class A {
vector<double> *data; // pointer, so objects of 'A' can _share_ data.
bool data_is_shared; // does this 'A' share data with another A?
A(const bool do_data_init=true); // ctor
A(const A& a); // copy constructor
A get_shared_A(); // get an object A which shares 'this' data
};
Ctor:
A::A(bool do_data_init)
{
if (do_data_init)
data = new vector<double>;
data_is_shared = !do_data_init;
}
Copy ctor, always result in an 'A' which does not share data:
A::A(const A& a)
{
data = new vector<double>;
data_is_shared = false;
*data = *a.data;
}
Get an object A which shares data with 'this':
A A::get_shared_A()
{
A a(false);
a.data = data; // copy the data pointer only
return a;
}
The problems focus on this last method 'get_shared_A'. It should return
an object A that shares its data with 'this'. Problem: on returning 'a',
the copy constructor _may_ be called and then an object A is returned
which has its own data vector. So this doesn't work... I cannot change
the copy ctor because that should always return an A which does not
share its data (that's a requirement for correct behaviour of class A).
In my application, 'get_shared_A' is an operator which selects a part of
the original data in 'this', and the returned result must be an object A
because it must be used in mathematical expressions just like all other A's.
So, using a 'helper'-class B I tried to change this in:
A A::get_shared_A()
{
B b;
b.data = data; // copy the data pointer only
return b;
}
With class B:
class B {
vector<double> *data;
};
And an extra conversion ctor for A:
A::A(const& B b)
{
data = b.data;
data_is_shared = true;
}
So I wanted the code to force to use this new ctor and return an object
A which shares its data. But again, after the conversion ctor is called
it is still possible that the copy ctor is also called and things do not
work correctly.
In fact, all possible solutions I came up with do not work because you
cannot be sure if the copy ctor is called when returning from
'get_shared_A', it depends on the compiler you have.
So, does anybody has any ideas to overcome this problem?
Jeroen
I'm trying to implement a certain class but I have problems regarding
the copy ctor. I'll try to explain this as good as possible and show
what I tried thusfar. Because it's not about a certain code syntax but
more a 'code architecture' thing , I'll use simple example classes
(which are certainly not complete or working...) just to illustrate the
idea (and I may make some mistakes because I'm not that experienced...).
The basic idea is that I have a class which has a data vector, but
objects of that class can also share that data vector between several
instances:
class A {
vector<double> *data; // pointer, so objects of 'A' can _share_ data.
bool data_is_shared; // does this 'A' share data with another A?
A(const bool do_data_init=true); // ctor
A(const A& a); // copy constructor
A get_shared_A(); // get an object A which shares 'this' data
};
Ctor:
A::A(bool do_data_init)
{
if (do_data_init)
data = new vector<double>;
data_is_shared = !do_data_init;
}
Copy ctor, always result in an 'A' which does not share data:
A::A(const A& a)
{
data = new vector<double>;
data_is_shared = false;
*data = *a.data;
}
Get an object A which shares data with 'this':
A A::get_shared_A()
{
A a(false);
a.data = data; // copy the data pointer only
return a;
}
The problems focus on this last method 'get_shared_A'. It should return
an object A that shares its data with 'this'. Problem: on returning 'a',
the copy constructor _may_ be called and then an object A is returned
which has its own data vector. So this doesn't work... I cannot change
the copy ctor because that should always return an A which does not
share its data (that's a requirement for correct behaviour of class A).
In my application, 'get_shared_A' is an operator which selects a part of
the original data in 'this', and the returned result must be an object A
because it must be used in mathematical expressions just like all other A's.
So, using a 'helper'-class B I tried to change this in:
A A::get_shared_A()
{
B b;
b.data = data; // copy the data pointer only
return b;
}
With class B:
class B {
vector<double> *data;
};
And an extra conversion ctor for A:
A::A(const& B b)
{
data = b.data;
data_is_shared = true;
}
So I wanted the code to force to use this new ctor and return an object
A which shares its data. But again, after the conversion ctor is called
it is still possible that the copy ctor is also called and things do not
work correctly.
In fact, all possible solutions I came up with do not work because you
cannot be sure if the copy ctor is called when returning from
'get_shared_A', it depends on the compiler you have.
So, does anybody has any ideas to overcome this problem?
Jeroen