private members

From: <>
Date: Fri, 21 Jan 2005 12:43:03 +0000 (UTC)

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

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


/************* function definition **********/

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/ root [1] .L testFunc.cpp++
Info in <TUnixSystem::ACLiC>: creating shared library /cern/ROOT/root401/mylib//home/simonyan/ROOT/leakage/functions/ 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.


        Margar Simonyan Received on Fri Jan 21 2005 - 10:03:43 MET

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