How to access subclass pointer that is Protected / Private Member

I have a queue.h file like the following. Is it possible that I can access the head pointer of the queue from Main?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>
using namespace std;

class PCB
{
    public:
        int PID;
        string fileName;

};


template<class T>
class myQueue
{

    protected:
        
        class Node
        {
            public:
                T info;
            
                 Node *next;
                Node *prev;
        };
        
        Node *head;
        Node *tail;
        int count;

    public:

        
        void getHead(Node **tempHead);
};



template<class T>
void myQueue<T>::getHead(Node **tempHead)
{
    *tempHead = head;
}


#endif 


my main is :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "myQueue.h"
#include <iostream>

int main()
{
    
    myQueue<PCB> queue;
    
    
    //How can I access the Head pointer of my Queue here?
    //queue.getHead(&tempHead);
    
    
    
    return 0;
}


Thank you in ahead
It is a gross violation of encapsulation; but if a you must:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <iomanip>
using namespace std;

class PCB
{
    public:
        int PID;
        std::string fileName;
};


template < class T > class myQueue
{
    protected:

        class Node
        {
            public:
                T info;
                Node *next;
                Node *prev;
        };

        Node *head = nullptr ;
        Node *tail = nullptr ;
        int count = 0 ;

    public:
        Node* getHead();
};

template<class T>
typename myQueue<T>::Node* myQueue<T>::getHead() { return head ; }

int main()
{
    myQueue<PCB> queue ;

    //How can I access the Head pointer of my Queue here?
    auto p = queue.getHead();
    if( p != nullptr ) std::cout << p->info.PID << '\n' ;
}


Strongly consider using std::queue<> or std::deque<> instead of a home-grown queue.
http://en.cppreference.com/w/cpp/container/queue
http://en.cppreference.com/w/cpp/container/deque
Dear JLBorges, thank you very much. Yup, you are right, it is violation and I should use deque. However, your code make me learn something new. Thanks again and have a nice weekend.
There are some types of queues in c++ and they are very recommended to use. Making your own one can lead to some errors that had already been fixed in the built in ones (within the std library). Of course you can use your just watch out from errors. If you need help with those kind of errors you can get some programs to help you, such as checkmarx or others, to detect errors.
Good luck!
Ben.
Topic archived. No new replies allowed.