0

Say you are given the following UML class diagram:

Mystery UML Class Diagram

Can a variable of type Mystery invoke the function DoSomething()?

I understand that an object (say Mystery X;) could call GetA() to access the private variable int a and to access the public variable int b all you need is X.b but how could this object, X, access the private function DoSomething() if it's even possible?

Marc Climent
  • 9,434
  • 2
  • 50
  • 55

3 Answers3

3

I had difficulty understanding exactly what you are asking, but I think I've figured it out.

If you are asking if, given the following declaration:

class Mystery
{
/*...*/
private:
  void DoSomething();
};

you can do something like this:

Mystery m;
m.DoSomething();

...then the answer is no. You cannot call private member functions (or refer to private member variables) from outside the context of the class. Only another member function of Mystery can call the privates. For example:

void Mystery::Foo()
{
  DoSomething();  // this would be possible if Foo() is a member of Mystery
}

EDIT:

Not only can you not call private members from outside the class, you also can't call them from subclasses. For example, this is not valid:

class Base
{
private:
    void Foo() {};
};

class Child : public Base
{
public:
    void Bar() 
    { 
        Foo();  // ERROR:  Can't call private method of base class
    }
};
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • Why you had difficulty? you've just read the answer from others and made up yours out of those. – Roman Byshko Nov 30 '11 at 20:02
  • I had difficulty understanding what the OP was asking because I found statements such as "can a variable call a private function" to be confusing. Variables do not invoke code. Code invokes code. In any case, through the help of the edit of the OP and the other answers given here, along with re-reading the OP several times, what the OP was trying to ask started to make sense. – John Dibling Nov 30 '11 at 20:07
1

There is one (edit - for completeness sake, there is more than one, see the comments) weird way that a private member function could be called outside your class, but it requires a little bit of a contrived example, where you can return a pointer to this function:

class Mystery;
typedef void (Mystery::*fptr)();

class Mystery{
    void DoSomething() {};
public:
    static fptr GetPrivateDoSomething()
    {
        return &DoSomething;
    }
};

int main(int argc, char *argv[])
{
    Mystery m;
    fptr ds = Mystery::GetPrivateDoSomething();
    (m.*ds)();
    return 0;
}

That being said, don't do this. Private methods are private for a reason - they embody hidden aspects of the design, and were never meant to be a part of the exposed interface. That means that they could be subject to a change in behavior, or even complete removal. Additionally, doing these sorts of shenanigans can lead to code that is highly and awkwardly coupled, which is undesirable.

THAT being said, I have indeed used this and it works quite well, although it was in an entirely different context where it helped to reduce coupling (it was in a highly-modular event registration scheme, if you were wondering).

Nate
  • 12,499
  • 5
  • 45
  • 60
  • True, but you've also missed friends. I don't think looking into the deep dark exceptions to rule is helpful here. – Winston Ewert Nov 30 '11 at 20:14
  • 1
    Okay, for one, he asks "if it's even possible". Did you down vote because I gave some trivia about how it's possible and you think that was wrong of me (at which point I might note that I explicitly said *not* to do it, and why!)? Or did you down vote because I didn't think of all solutions?? My method is a simple one, and can be used anywhere in the program. You would have to `friend` every function in the program to do the same. – Nate Nov 30 '11 at 20:21
  • I'm not the downvoter, I don't think you deserved a downvote. My only issue with your post is that you started out with "I'm disagreeing with everyone else," and I was pointing out that the other didn't comment on it because it serves to complicate an issue the OP seemed already confused about. The way you started makes it sound like you thought everyone else was wrong because they didn't mention this. – Winston Ewert Nov 30 '11 at 20:39
  • Ah... they happened at nearly the same time, so I misconnected the dots. And though your answer doesn't state it, the accepted answer as well as another that has since been deleted both said that it could not be done, hence the "disagree". I have removed that language, as it is less applicable. – Nate Nov 30 '11 at 20:45
  • Well... in C++ if you try hard enough you can get around any rule. I wish they'd make down-voters leave a comment. – Winston Ewert Dec 01 '11 at 04:36
0

Any method inside the class is allowed to access the private variables and methods. It works exactly like calling any other method, its just that the compiler will give you an error if you try to do it from outside the class.

Winston Ewert
  • 44,070
  • 10
  • 68
  • 83