Reference, Copy C'tor etc.

matrix_columns MUST point on the same vector as matrix_rows.

Copy constructor copy the vector: unacceptable.
Move constructor destroy vector of matrix_rows: unacceptable.
memcpy in code below, does the right thing.

Is there a better approach? buffer of chars is a little bit crapy coding.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Matrix : public vector<Vector> { bool vertical; };
int main()
{
	Matrix matrix_rows;
	matrix_rows.vertical = false;

	char buffer[sizeof(Matrix)];
	memcpy(buffer, &matrix_rows, sizeof(Matrix));

	Matrix &matrix_columns = reinterpret_cast<Matrix&>(*buffer);
	matrix_columns.vertical = true;

	// do things.

	return 0;
}
Your code is incorrect if Matrix is not a POD struct. Only POD's can be cloned using memcpy().
I think, you are wrong.

Your problem exists only in virtual classes or classes with multiple inheritance:
- With multiple inheritance reinterpret_cast blows the universe.
- If you upcast with single inheritance, reinterpret_cast keeps wrong virtual table.

I don't use virtual class.
I don't use multiple inheritance.
I don't use upcasting.

So, I believe I am ok.
Wrong?
Last edited on
I have never delved that deeply into the issue so I cannot really assure you it is wrong even under special circumstances. What I CAN say is that bitwise cloning is for POD's only; non-POD's must use memberwise cloning. This is my instruction talking and I have never read about any exceptions anywhere. I'm not the theorist guy that can prove or disprove your theory, so hopefully someone else can stop by and collaborate.
Wrong?
Yes (sorry couldn't resist)

the problem with the virtual table is that it is a pointer. A non POD type like vector has likely more pointer. If you copy those pointers the memory pointed to is probably invalid. It would rather be a surprise if not.

Like webJose said don't copy non POD types. Instead overload the operator=(). And you shouldn't use a STL container as a base class.

reinterpret_cast is horrible don't use that. Only dynamic_cast is safe.
Last edited on
Ooops!!
I forget to say, that since I add needed elements to vector, class stay const (no changes at all, until destruction). So then I create a reference to that vector (only difference is orientation of vectors - the member vertical).

All of these, you say, are correct.
If you copy the 3 pointers of vector, and you remove an element from that vector, these 3 pointers become invalid. But if you don't change vector, the 3 pointers remain valid.
It's still undefined behavior and therefore incorrect. Even if it seems to work now, it doesn't mean it will work tomorrow or with a different compiler or with different compiler settings or when it starts raining outside.

If you need different views on the same object, then create a MatrixView class that contains some sort of pointer to the real Matrix. And if the original object is constant anyway, then how exactly is copying the vector "unacceptable"?
Last edited on
Topic archived. No new replies allowed.