0

Lets say I have a class "ClassA". Is it possible to assign a pointer of another instance of the class to a non-pointer variable? for example

ClassA pineapple();
ClassA* replacementPineapple = new ClassA();
pineapple.refersto = replacementPineapple; <- something like that

The reason I'm asking is because I have a class where I need to move a lot of the class variables to be physically located in a memory mapped file. I could of course just have them as pointers and dereference them every time i need to use them but thats a lot of dereferencing and with all the brackets and other stuff just makes the code really hard to read. If there is any way around that I'll take it.

Jake Freelander
  • 1,471
  • 1
  • 19
  • 26
  • 1
    No, you can't. The most similar thing to what you are looking for are references, but they cannot be reseated (i.e. the location they point to must be known at their creation). – Matteo Italia Jul 07 '14 at 09:05
  • If you need just move, so, get class at destination and remove it from where it was, you can simply use default copy constructor or memory copying. If there was some POD data - it will be copied, if there was references - you will copy them, and they will still point to right memory parts. And then free memory where this class was. But you have to avoid call destructor for that, to not release pointed subobjects. – Arkady Jul 07 '14 at 09:08
  • 3
    This is a classic X-Z problem. Please state the actual problem you are trying to solve, or rather, state a minimalistic representation of the problem you're trying to solve. – Kerrek SB Jul 07 '14 at 09:12

3 Answers3

0

Yes it is. You can allocate the memory flat and then do the copying (from an existing object) by hand. Or you use placement new operator to allocate/construct at a given location.

see: using placement new

Community
  • 1
  • 1
ogni42
  • 1,232
  • 7
  • 11
  • Maybe I didn't understand OP's problem, but it seems that he only wants some syntax sugar to avoid pointer notation... – Matteo Italia Jul 07 '14 at 09:22
  • Yes it sounds like that. And using placement new will solve this, as you can create the object at a specific position, create a reference to it and access this like any regular object. – ogni42 Jul 07 '14 at 09:33
0

I need to move a lot of the class variables to be physically located in a memory mapped file. I could of course just have them as pointers and dereference them every time i need to use them but thats a lot of dereferencing and with all the brackets and other stuff just makes the code really hard to read. If there is any way around that I'll take it.

class ClassA
{
  public:
    ClassA(int* p_a, double* p_d) : a_(*p_a), d_(*p_d) { }

    int& a_;
    double& d_;
};

Then you can create an instance like this:

ClassA my_a(ptr_to_a_in_shmem, ptr_to_d_in_shmem);

And write code (member functions or otherwise) that opererates on a_ and d_ thereby accessing/modifying shared memory.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0

If I understand your question correctly you just want to use pineapple as an alias for *replacementPineapple. You can do this by first creating the latter, and defining pineapple to be of reference type (marked by &)

ClassA* replacementPineapple = new ClassA();
ClassA& pineapple = *replacementPineapple;

Now every (read or write) access to a member of pineapple will actually access the corresponding member of *replacementPineapple. Only if you should at some time modify the pointer replacementPineapple to point elsewhere (or become null) then pineapple will still refer to the object it originally pointed to, and there is no way you can make it follow.

Marc van Leeuwen
  • 3,605
  • 23
  • 38
  • The physical location of the data has to be in a mmf and due to having to dynamically resize it can change so references aren't really an option. – Jake Freelander Jul 07 '14 at 11:14