-3

Excuse me, if I'm repeating a question, but I do not find the same. What are right actions to extract an element from the standard queue?

T Pop ()
{ T rout;

  EnterCriticalSection (pCSection);
  rout = Queue.front (); // here the link is returned
  Queue.pop ();          // here destructor is called
  LeaveCriticalSection (pCSection);

  return rout; // -->> there everything falls
}

I have a class T with dynamic memory management and copy constructor, in the line with front() the copy constructor is not called automatically.
Destructor frees the memory at the end of the scope, and I have the entity with garbage.

How can I force the copy constr?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Kirill Golikov
  • 1,354
  • 1
  • 13
  • 27
  • 3
    Does `T` provide a proper copy constructor for your actual case? _"Maybe force the copy constructor?"_ sounds like a pretty good idea. Also _"there everything falls"_ isn't a very good description of your actual problems. – πάντα ῥεῖ Nov 22 '14 at 22:00
  • You should check the queues size, before calling `Queue.front();` BTW. – πάντα ῥεῖ Nov 22 '14 at 22:01
  • The entire code is useless if T is a pointer. –  Nov 22 '14 at 22:02
  • "Everything falls" is a joke. I have a class T with dynamic memory and copy constructor, in the line with front() the constructor of coping is not called automatically. Destructor frees the memory, and I have the entity with garbage. How can I force the copy constr? – Kirill Golikov Nov 22 '14 at 22:03

2 Answers2

1

"I have a class T with dynamic memory and copy constructor, ... How can I force the copy constr?"

That's easy

T rout(Queue.front());

Most probably you've been missing to provide a proper assignment operator for T along. See also here please What is the Rule of Three?


Also as you're mentioning dynamic memory management done in your class T. Ease up yourself please, and use standard container classes, or even standard dynamic memory management features of c++ preferably to use new and delete directly.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
1

Is your problem that you don't have a copy assignment operator? You mention the copy constructor, but you're not copy constructing anything in your code. Perhaps you meant to do:

T Pop ()
{ 
    assert(!Queue.empty());

    EnterCriticalSection (pCSection);
    T rout = Queue.front ();
    Queue.pop ();
    LeaveCriticalSection (pCSection);

    return rout;
}

Or even:

    T rout(std::move(Queue.front()));
Barry
  • 286,269
  • 29
  • 621
  • 977
  • I'm interesting in pattern of extracting elements from std::queue, because this class returns the links only. Thank you, now I understand - there are two ways 1. redefine the operator=, 2. force the copy constructor. Wonderful! – Kirill Golikov Nov 22 '14 at 22:15
  • I'm afraid a _real world solution_ would get a bit more complicated: 1. The `assert()` doesn't really help much in reality (unless checked beforé calling `Pop()`). 2. The check for `!Queue.empty()` should be done inside the _critical section_ 3. If the check is done inside the function, one has to decide, if` `T()` should be returned as a dummy value, or an exception should be thrown. – πάντα ῥεῖ Nov 22 '14 at 22:37