On Mon, Mar 9, 2009 at 8:50 AM, Tim Kroeger
<[email protected]> wrote:
> Dear all,
>
> What do you guys think about having assignment operators in DenseSubVector
> and DenseSubMatrix? Actually, I would like to have them since that would
> enable me to use a std::vector<DenseSubVector>. This requires the
> DenseSubVector::parent member to be a pointer rather than a reference, but
Why does it need to be a pointer? AFAICT it was not a constant
reference so it can be changed at op=. The following test code
compiles OK for me. The fact that it's a reference reflects the fact
that the parent of a dense sub matrix/vector should not be NULL; I'd
like to keep those semantics if possible.
#include <iostream>
class Parent
{
public:
Parent(int v) : value(v) {}
int value;
};
class Child
{
public:
Child(Parent& p) : parent(p) {}
Child& operator= (const Child& other)
{
parent = other.parent;
}
Parent& parent;
void print() { std::cout << parent.value << std::endl; }
};
int main()
{
Parent p1(1), p2(2);
Child c1(p1);
c1.print();
Child c2(p2);
c2.print();
c2 = c1;
c2.print();
return 0;
}
> that shouldn't cause any problems, right? Actually, I just implemented this,
> so if you like it, just check it in.
It looks like the current code handles self-assignment just fine, but
to be pedantic we should probably check for self-assignment in all our
operator= calls and return *this if self-assignment occurs. Basically
just something like:
Foo& operator=( const Foo& other )
{
if ( &other != this )
{
// perform copy steps
}
return *this;
}
Also, am I crazy or was there an "Effective C++" about implementing
copy ctors in terms of assignment operators (or vice-versa)? That way
you avoid some code duplication in the two functions which are kind of
doing the same thing. I've been searching but haven't found exactly
what I'm thinking of in the FAQ or any other places...
--
John
------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Libmesh-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libmesh-devel