Thread: how to access a private member function witout using friend or virtual

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    Post how to access a private member function witout using friend or virtual

    Code:
    hi ppl,,,
    
    i am new to programming,,, pls help me out... i need to access a private member function of a class witout using virtual or friend and i should not alter the class but i can do anything,, pls dont say #define private public ...
    
    try to give some better solution and help me here
    
    eg:
    
    class X
    {
         void show(){ }
    public:
        ........
    };
    
    i need to access that show() function witout modifying the class X 
    help me out pls

  2. #2
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Well you can use the getter/setter method approach.

    Code:
    class A
    {
      public:
          inline int getTheInt() const
          {
                return m_TheInt;
          }
    
          inline int setTheInt(const int theInt)
          {
                m_TheInt=theInt;
          }
      private:
          int m_TheInt;
    };
    BTW, why do you use the code tag for the whole post? Only use the code tag for the particular code.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    Thumbs down i should not modify the original class

    hi thanks for replying,,

    my question is that i should not modify the orignal class and inheritance should be avoided

    i need to access the private function not the integer inside the class

    Code:
    
    

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The short answer is that you can't. Making a member function private is designed to ensure that only a friend or another member function of the class can call it.

    There are a few hacks (eg the "#define private public" that you mentioned). But all of those hacks (1) invoke some form of undefined behaviour, so (2) are not guaranteed to work as you intend.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    The function is private to the class and is non virtual. You can't access it from outside the class it is declared. And inheritance wouldn't help either, no matter the type; public, private, virtual.

    It is simply not possible without altering the class. However, you may find that your class public area may access the function somewhere. That is how you normally access private functions.

    EDIT: the macro hack wouldn't work with the code provided either.
    Last edited by Mario F.; 01-09-2007 at 04:28 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    8

    Post u guys are rite....

    you guys are right...

    i did this

    Code:
    class A 
    {
    public:
    	int a;
    	A()
    	{
    		a=100;
    	}
    private:
    	void show()
    	{cout<<"A\n";}
    };
    
    class B 
    {
    	int a;
    public:
    	void show()
    	{cout<<"B\n"<<a<<"\n";}
    };
    
    void privateFunction() 
    {
    	A x;
    	B *y;
    	A *a;
    	
    	a = &x;
                    x.a=50;
    	y=(B *)a;
    
    	y->show();   // i got a output as B 50
    }
    i got tat value of a in the class A but the function call goes to the class B.. is there any way that i could make the function in class A could be called

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Notice the only reason your cast worked is because both classes have similar data members.

    Anyways, you can only make show() callable from outside a class where it declared as private, by making it a friend of another class. You can also make it protected, in which case, it will "spawn" as a protected function of a public or protected derived class, or private of a private derived class.

    In other words B must derive from A or be a friend of A. If B derives from A, A::show() must be protected in order to keep it private to users of A and yet accessible from within B (not users of B).

    A final option is to declare A::show() as protected and virtual. And have B derive from A. In this case, you can declare B::show() as public and call A::show() from within.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    If you feel the need to access a private function or variable outside the class, then your design is wrong. It's as simple as that. Either you're trying to do something that shouldn't be done (using the class as it was not intended to be used) or your class's interface is not powerful enough to do all the things that could in theory be done.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Mario F.
    Notice the only reason your cast worked is because both classes have similar data members.
    .... and also, more significantly, because the layout of the two classes (A and B) in memory happen to be the same with your compiler. Problem is, there is nothing stopping a compiler from laying out the data in class A differently from that data in class B; another compiler could well do it differently.

    The reason the explicit cast is needed in the line "y = (B *)a;'" is because it is necessary to bludgeon the compiler into submission so it doesn't complain about the conversion. The reason it will (untl bludgeoned) complain about the conversion is that any subsequent dereferencing of y, such as the call y->show(), will yield undefined behaviour.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Why don't you just make those members public? Private access only detects if you are trying to use members you don't want to use (quite the same as const).
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Super hacky but you could try to find out the address of the function in the executable and then do some inline asm

    Code:
    _asm
    {
    	mov eax, 0x0040101E;
    	call eax;
    }
    or if you have control over the class, print the function address from within the class first, then remove the code and fill in the address in the asm

    Code:
    void (A::*p)() = A::Show;
    
    printf("%p\n", p);
    This code presumes the function returns void without parameters

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Boy, this question is sure doing the rounds - it's good that most everyone is saying the same thing.

    http://forums.devshed.com/c-programm...on-415558.html
    http://www.daniweb.com/techtalkforums/thread66417.html

    Perhaps it's time to realise that your class model is broken and that there is no real way around it except to redesign the thing properly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is weird that you so desperately want to find a way to break the language rules.

    The name show() indicates that this method should be public - well, there's something public about showing.

    If, for some reason you want to restrict access to this method to only one function or class, you can declare it as a friend in class A.

    Code:
    #include <iostream>
    
    class A;
    class B
    {
        public:
            void showA(A a);
            void noFriends(A a);
    };
    
    class A
    {
        public:
            A(int i): x(i) {}
        private:
            int x;
            void show()
            {
                std::cout << "The number is " << x << '\n';
            }
        friend void B::showA(A);
    };
    
    
    int main()
    {
        A a(42);
        B b;
        b.showA(a);
        //a.show(); //error: A::show() is private in this context
        std::cin.get();
    }
    
    void B::showA(A a)
    {
        a.show();
    }
    
    void B::noFriends(A a)
    {
        //a.show(); //error: A::show() is private in this context
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why private virtual function?
    By George2 in forum C++ Programming
    Replies: 5
    Last Post: 03-16-2008, 05:03 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM