# Can I use private variable by shifting address?

**URL:** <https://root-forum.cern.ch/t/can-i-use-private-variable-by-shifting-address/62452>\
**Category:** Newbie\
**Tags:** root\
**Created:** [December 12, 2024, 5:19am UTC](https://root-forum.cern.ch/t/can-i-use-private-variable-by-shifting-address/62452 "2024-12-12T05:19:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [December 12, 2024, 5:19am UTC](https://root-forum.cern.ch/t/can-i-use-private-variable-by-shifting-address/62452/1 "2024-12-12T05:19:18Z")

</div>

Dear experts

For debugging purpose, Can I call a private member in a Class by shifting the address of the Object pointer? Where the class is inherited from `TObject`:

```auto
class SomeClass : public TObject
{
    private:
        typedef std::map<std::string, int> A;
        typedef std::vector<std::string> B;
        TFile* fFile;
        TTree* fTree;
...
};

```

Is there a way to access `fFile` by shifting the address of pointer of theobject of `SomeClass`?

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [December 12, 2024, 7:33am UTC](https://root-forum.cern.ch/t/can-i-use-private-variable-by-shifting-address/62452/2 "2024-12-12T07:33:39Z")

</div>

Hi,

Yes. This is also the way ROOT manages to serialise objects internally. However, this is something I would not advise to do if not forced to: it is always better to design well the classes rather than resorting to these measures. Below you find an example of how this can be achieved using ROOT’s type system (for the sake of the example, I used public data members to show their addresses):

```cpp
class SomeClass : public TObject {
public:
   typedef std::map<std::string, int> A;
   typedef std::vector<std::string> B;
   TFile *fFile = new TFile("a.root", "recreate");
   TTree *fTree;
};

void a()
{

   auto cSomeClass = TClass::GetClass("SomeClass");
   const auto fFileOffset = cSomeClass->GetDataMemberOffset("fFile");
   auto c = new SomeClass;
   cout << c << endl << &(c->fFile) << endl;

   cout << fFileOffset << endl;
   auto fp = (TFile **)(fFileOffset + (Longptr_t)c);
   auto f = *fp;
   cout << fp << endl << f->GetName() << endl;
}

```

Best,  
D

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [December 12, 2024, 12:43pm UTC](https://root-forum.cern.ch/t/can-i-use-private-variable-by-shifting-address/62452/3 "2024-12-12T12:43:14Z")

</div>

Thank you for the answer, but I want to access a private variable in such class structure, which means I can’t use `GetDataMemberOffset()`, is there a way to do that…?

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [December 12, 2024, 1:50pm UTC](https://root-forum.cern.ch/t/can-i-use-private-variable-by-shifting-address/62452/4 "2024-12-12T13:50:18Z")

</div>

Hi,

I do not understand what is exactly the reason why you cannot use ROOT’s type system. The example above shows how to access a private variable in such class structure (I use public for the sake of the example, the memory layout is the same if you replace it with public).  
You can of course not use ROOT to do such a thing. That would mean following the procedure above but figuring out or hardcoding the offsets by yourself in bare C++.

Cheers,  
Danilo

---

<div class="post-metadata">

**Author:** ![Crisps](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/crisps/32/39101_2.png) [@Crisps](https://root-forum.cern.ch/u/Crisps)\
**Post date:** [December 12, 2024, 2:08pm UTC](https://root-forum.cern.ch/t/can-i-use-private-variable-by-shifting-address/62452/5 "2024-12-12T14:08:32Z")

</div>

Oh sorry I misunderstood your answer, yes you are right!

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [December 12, 2024, 4:25pm UTC](https://root-forum.cern.ch/t/can-i-use-private-variable-by-shifting-address/62452/6 "2024-12-12T16:25:39Z")

</div>

Perfect, no problem. Let us know how this goes!

D
