The goal here is to have some other object in MyClass which always needs a reference to the "stuff" in the same object. I'd like to develop a way of automatically updating this on copies/moves which doesn't require writing an entire copy constructor for MyClass. If I can fill RelativeLoc with callbacks or something, it would fit the bill.
The goal here is to have some other object in MyClass which always needs a reference to the "stuff" in the same object. I'd like to develop a way of automatically updating this on copies/moves which doesn't require writing an entire copy constructor for MyClass. If I can fill RelativeLoc with callbacks or something, it would fit the bill.
As long as relative and stuff are in the same struct (or struct hierarchy), than that code seems perfectly safe to me.
As long as you use RelativeLoc in the way described here, both the default constructor and assignement operator should be fine. Heck, I'd consider making offset a const member, and re-defining the assignment operator as do nothing.
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
That was my first reaction, but this doesn't work. The reason is that when you copy MyClass, you want the new stuff_ptr to point to the new stuff. The only way to do that is to give the address of stuff to the stuff_ptr, and hence, requires writing a copy constructor. By having the "Reference" keep an offset, you don't need to write a copy constructor, since the offset is the same for all instances of MyClass.
What lindley wrote seemed fishy to me at first, but it works. I'm more curious about why you would want suf a construct to begin with, and if this construct would work with his need.
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Re: Automatically obtaining the address of another member of the same object
Originally Posted by monarch_dodra
That was my first reaction, but this doesn't work.
It sure works. The stuff_ptr() function will always return a pointer to stuff of the current object.
What you cannot do is assign stuff_ptr() to an object variable and then copy the object. Then the variable of the copy will point to stuff of the object it was copied from. So you must always access stuff via a call to stuff_ptr() and never store a pointer to stuff in an object variable. But the same goes for Lindley's getOtherAddr() function so there's no difference really. Lindley's suggestion is just more complicated, more convoluted and more error prone due to ugly pointer arithmetics and not typesafe casts. In short exactly the kind of coding one should avoid.
Last edited by nuzzle; October 27th, 2010 at 05:01 AM.
Re: Automatically obtaining the address of another member of the same object
Originally Posted by nuzzle
It sure works. The stuff_ptr() function will always return a pointer to stuff of the current object.
What you cannot do is assign stuff_ptr() to an object variable and then copy the object. Then the variable in the copy will point to stuff of the object it was copied from. So you must always access stuff via a call to stuff_ptr() and never store a pointer to stuff in an object variable. But the same goes for Lindley's getOtherAddr() function so there's no difference really. Lindley's suggestion is just more complicated.
I missread your code, sorry. I though stuff_ptr was actually a member variable initialized to stuff. Didn't see it was actually a function.
I'm still not sure what Lindley is trying to do, so I can't judge either approach.
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Re: Automatically obtaining the address of another member of the same object
I'm trying to use libkdtree++ to optimize a program. However, I don't want to take the space to put the actual objects into the KDTree----instead I want to put indexes in and provide the tree with a dimension accessor that applies the indexes to the vector.
This is all fine so long as the underlying vector doesn't move. But if the object containing both the KDTree and the vector is copied or moved, then I have a problem----the KDTree's accessor is no longer valid.
While the KDTree does not allow its accessor to be modified for understandable reasons, I *can* copy-construct another one with a different accessor. It's probably a waste of cycles since the tree structure is already known, but I'll accept that.
The query is, can I update the pointer without having to write cctor, mctor, and operator= (both versions) for the enclosing object? There are unrelated fields in there too.
Another possible solution besides the above would be to wrap up the vector and the KDTree in a smaller object and give this object and operator[] which makes it behave as a vector, as well as the appropriate KDTree functions.
Re: Automatically obtaining the address of another member of the same object
No, because when the RelativeLoc object is copied (within the copy of the surrounding object), it would still have the reference to the previous other object, not the new one.
Re: Automatically obtaining the address of another member of the same object
I'm still not clear what exactly are you trying to achieve, can you show us an actual example?
Just an observation:
When you copy an object, the address of the data member never changes.
Code:
void f()
{
C1 c1;
C2 c2;
c1=c2;
}
Only the values within the data members are manipulated, but nothing is actually moved around. Granted, if you re-allocate a vector then the addresses will change. As monarch_dodra implied the offset will be the same for every object. There must be a simpler way of doing this.
Last edited by Zaccheus; October 27th, 2010 at 11:31 AM.
Re: Automatically obtaining the address of another member of the same object
Originally Posted by Zaccheus
There must be a simpler way of doing this.
Sure----just implement the copy constructor. I'm just playing around with ideas for alternatives in the case when *most* of the object doesn't need special copy treatment.
I'm still not clear what exactly are you trying to achieve, can you show us an actual example?
This is the basic idea, except with a simple sort rather than a KDTree, and only 1-D data (doubles). Now, put the Sorted object into another object alongside the actualvals:
Re: Automatically obtaining the address of another member of the same object
Originally Posted by Lindley
Is this safe?
In general, I don't think it is safe. In practice, I think it is safe as long as the two objects are stored in the same struct/class (i.e. not class hierarchy).
The construct is very similar to the C macro offsetof, which is only supported in C++ for POD types. Here's a thread about why it is, in principal, not safe to use offsetof on non-POD types.
What you are doing seems even safer than offsetof, because you are not relying on compile time offsets, but only that the offset between two member variables in a struct/class is equal for every instance of that struct/class. Though I don't think that's guaranteed by the standard, I would expect that to be true for 99.9% of the C++ compilers out there.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Re: Automatically obtaining the address of another member of the same object
Originally Posted by D_Drmmr
What you are doing seems even safer than offsetof, because you are not relying on compile time offsets, but only that the offset between two member variables in a struct/class is equal for every instance of that struct/class. Though I don't think that's guaranteed by the standard, I would expect that to be true for 99.9% of the C++ compilers out there.
I don't know what the standard says, but it would seem odd to me iff every object of a class did not have the same memory layout. How would you reference the attributes of that object?
-----
Back to the subject: Your first provided implementation looks safe to me, provided you use it as described of course. I'd consider some tweaking though. For starters, I think the constructor should take a reference, and not a pointer. This would save you from ever constructing a RelativeLoc from a nullptr.
Also, instead of giving a getOtherAddr function, just give your class a pointer interface. Something like this:
It just needs some extra tweaking for const correctness...
Is your question related to IO?
Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Bookmarks