1

I guess I've fallen into a situation that I need to modify something that is "protected" in some sense. I worked around and didn't find a proper solution, or it is actually unsolvable.

A.h

class A
{
    static void append(int x);
}

A.cpp

class B;
static B *queue = 0;

class B
{
    friend class A;

    int value;
    B* next;

    B(int x)
    {
        value = x;
        next = queue;
        queue = this;
    }
}

void A::append(int x)
{
  new B(x);
}

What I want to do is basically finding a way to manipulate the queue externally in another source file without changing A.h and A.cpp, since class A and B don't provide methods to manipulate the queue.

C.cpp

#include "A.h"

<whatever magic declaration>

void C()
{
    queue = 0;
}

Thank you very much for any comments!

  • Using `static` means that `queue` isn't visible in other translation units. That's the entire point of that use of the keyword. – chris Jun 15 '16 at 17:26
  • 3
    Your objective is analogous to: "I have a secret bank account. I don't anybody to know about it but I want some people to be able to put money in it." – R Sahu Jun 15 '16 at 17:26
  • If you can live with changing `A.h, A.cpp`... then you can always use the function `B& getQueue() { return *queue; }` which you define inside `A.cpp` and declare inside `A_priviliged.h`. But really, you should be asking yourself how you got yourself into this situation. – Centril Jun 15 '16 at 17:43
  • Someone else wrote this code and maybe he was wishing that the queue is only initiated once, and the code package has been distributed everywhere. I'm adding functionality to reset the whole context. Then I sadly found that the whole context includes the queue and it turned out to be a big problem for me. – Shunning Jiang Jun 15 '16 at 18:00
  • Being static queue is only available in class b. queue is assigned to next in class b. so if you want the value of queue you should first initiate d b than retrieve it using b.next or was it b->next. – Fuseteam Jun 16 '16 at 00:49

2 Answers2

4

There is no way to access queue outside of A.cpp. static gives the variable internal linkage, making it invisible outside the translation unit. Period.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • Maybe I'm overlooking something, but what about creating a function that returns a reference to the pointer and then exporting that function in the header. Then you can abuse the fact that a reference returned by a function is an l-value. And l-values can be assigned. Like so: `B& getQueue() { return *queue; }` – Centril Jun 15 '16 at 17:35
  • 2
    @Centril How will you do that "without changing A.h and A.cpp" (which is stipulated in the question)? – Angew is no longer proud of SO Jun 15 '16 at 17:36
  • oh, I missed that part of the question... Thanks for saving me from an embarrassing SO answer ;) For those interested,here's the stuff about l-values: // http://stackoverflow.com/questions/6111905/c-is-return-value-a-l-value – Centril Jun 15 '16 at 17:38
  • alright, I have to find other ways to solve the problem lol. Thank you very much! – Shunning Jiang Jun 15 '16 at 17:43
0

You cannot access this variable directly from your code - there is no "magic syntax" which allows this. However there is a way around - you can use debugging information generated by compiler. By using it you should be able to access that variable like debugger does. After quick Google search I found libgdb. You can try to use it to access that variable, or at least as an example how to load and use debug information.

Daniel Frużyński
  • 2,091
  • 19
  • 28