private members

From: <margar_at_atlas.yerphi.am>
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
#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 - 10:03:43 MET

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