RE: private members

From: Philippe Canal <pcanal_at_fnal.gov>
Date: Fri, 21 Jan 2005 09:03:49 -0600


Hi,

Within a ROOT dictionary, we had to bend the rule a little bit in order to properly implemented the I/O (the I/O itself has to sligthly break the data incapsulation since it needs to modify directly the data members). Since the file compiled by ACLiC are included within their dictionary, this leads to the behavior you have observed.

Cheers,
Philippe.

-----Original Message-----
From: owner-roottalk_at_pcroot.cern.ch [mailto:owner-roottalk_at_pcroot.cern.ch] On Behalf Of margar_at_atlas.yerphi.am
Sent: Friday, January 21, 2005 6:43 AM
To: roottalk_at_pcroot.cern.ch
Subject: [ROOT] private members

Dear all

I found that it is possible to manipulate class private data members outside the class within ROOT compiled code. See bellow the shortest code reproducing the problem.

/**************** class definition *********/

#ifndef A_h
#define A_h
#include<iostream>

class A {
  public:
             A() { i = 0; }
           ~A() { }
  private:
         int i;

};
#endif

/************* function definition **********/
#include"A.h"

void testFunc()
{

    A a;
    a.i = 5;
    std::cout<<"a.i = "<<a.i<<std::endl; }

I compile both the class and the function and execute the function.

root [0] .L A.h++
Info in <TUnixSystem::ACLiC>: creating shared library /cern/ROOT/root401/mylib//home/simonyan/ROOT/leakage/classes/A_h.so root [1] .L testFunc.cpp++
Info in <TUnixSystem::ACLiC>: creating shared library /cern/ROOT/root401/mylib//home/simonyan/ROOT/leakage/functions/testFunc_cpp. so
root [2]
root [2] testFunc()
a.i = 5

Of course it is impossible to compile testFunc.cpp with g++ because A::i is private member.
Usually people ask why something does not work, but this is not the case. I would like to know why and how this code works ? It violates data incapsulation, which is one of the main principle of Object-Oriented programming.

Thanks,

        Margar Simonyan Received on Fri Jan 21 2005 - 16:05:27 MET

This archive was generated by hypermail 2.2.0 : Tue Jan 02 2007 - 14:45:04 MET