0

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;
    }
}
10
  • 2
    Why is your constructor manipulating another object in the first place? That seems...rude. A constructor should focus on preparing the object. Another function should deal with linking it in with other structures. It's a separation of concerns issue here. Commented Apr 9, 2021 at 22:01
  • 1
    Unrelated: this is actually really weird. It's not const, well not unless the method itself is const, it's what's called a prvalue. Here's some good reading. Commented Apr 9, 2021 at 22:01
  • 2
    You might be mistakenly thinking you need a non-const reference to a pointer in order to do something like queue->front = ...;. You don't, that's what regular, undecorated pointers do. Changing front requires one level of indirection, not two. Commented Apr 9, 2021 at 22:02
  • 1
    I know what it does, I'm just saying that is not the job of a constructor. This function has assumed too much responsibility. Normally constructors construct and another function handles insertion. Commented Apr 9, 2021 at 22:04
  • 2
    I'd go a bit futher than that. Element shouldn't know enough about Queue to directly interact with Queues 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. Commented Apr 9, 2021 at 22:09

1 Answer 1

2

Just pass a simple pointer to the Element() constructor:

Queue::Element::Element(Queue* queue, int x)

The only thing you need to assure, is that Element can access Queue::front and Queue::back. For instance, if they are private members of Queue, then you can make Element a friend of Queue, something like:

class Queue {
//...
friend class Element;
//...

Edit: haven't noticed that you do modify Queue inside Element.

Sign up to request clarification or add additional context in comments.

8 Comments

I want to modify Queue object pointers. To modify them i can't pass Queue* queue (pointer) because it will make changes in function (local not global)
Please explain your goals more clearly. See: XY Problem.
@Kawson - once you pass a pointer to Queue into Element(), the changes you make on the pointed object will not be function-local, but will modify your Queue object. That's how passing pointers as function parameters works - you cannot modify the address of this object passed to a call to Element(), but you can modify the memory at that address (so the contents of Queue object).
@Tomek Oh.. If it does, it solves my problem. I just wanted to modify Queue object variables (not address as you said).
That's a very confused description of what's going on, so you may want to pull back and think about what you're doing at a higher level instead of charging ahead on the assumption you need to do things a particular way. You'll often find that when you're going against concerns you end up with highly convoluted, very intertwined code, which you can untangle by thinking about it a different way and organizing things better.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.