The question is not very clear, because I know that 'this' is const* and I can't modify pointer. I have class Queue and struct Element inside. I have constructor for Element which asign value and pointer to next element. I want to make function Push in Queue class which just create Element object (and pass value to Push function). My Element constructor is
Element(Queue*& queue, int value)
i must pass Queue object, because in Queue class i have pointer to first and last element of Element structure. So it have to modify my Queue object. My Push function:
Element* x = new Element(this, x); [i know that this can't work as i said this is const]
main:
Queue* q = new Queue();
q.Push(5);
How to pass object 'q' as parameter to constructor of Element?
EDIT: Element constructor:
Queue::Element::Element(Queue*& queue, int x)
{
if (queue->front)
{
Element* tmp = queue->front;
while (tmp->next)
{
tmp = tmp->next;
}
tmp->next = this;
this->value = x;
}
else
{
queue->front = this;
queue->back = this;
this->value = x;
}
}
thisis actually really weird. It's notconst, well not unless the method itself isconst, it's what's called a prvalue. Here's some good reading.queue->front = ...;. You don't, that's what regular, undecorated pointers do. Changingfrontrequires one level of indirection, not two.Elementshouldn't know enough aboutQueueto directly interact withQueues internal state (member variables). This couples the two classes far too tightly for my tastes. This is a sign of extremely weak encapsulation, and if you're writing this for a class you may get docked marks. If you're writing this for production, it shouldn't survive a code review. If you're writing this for your own entertainment, do as thou wilt.