1

Following is an interface:

class SIM{
private:
//private data

public:
Send();
Display();
Recieve();
Encrypt();
};

How do I restrict access to Display() function (it has to be lie in public part) of SIM to other classes except one class (Neo etc). I don't want to use friend etc.

Edit:

I can move the display() to private , how do i allow only NEO class to access it? 0_o

  • 4
    Why don't you want to use friends? – MGZero Jun 30 '11 at 14:38
  • If `Display()` has to be in `public` access then, anyone can access it regardless of any trick! This won't be possible. – iammilind Jun 30 '11 at 14:41
  • 4
    Friends are the appropriate solution here. This is what they were designed for. Without that, this is impossible. Your question doesn't make much sense. – Cody Gray - on strike Jun 30 '11 at 14:42
  • So is there any way that i move display() to private , then only particular class Neo only can access it? –  Jun 30 '11 at 14:43
  • 1
    @M3ta: Well, you exclude the exact mechanism that is designed to solve this problem. Can you explain why you cannot use friends, and why the method has to be public? – Björn Pollex Jun 30 '11 at 14:43
  • it seems that you know its not possible but you are searching for some hack. – Vijay Jun 30 '11 at 14:44
  • If you want `Display()` to be in `private` then change your question accordinglhy – iammilind Jun 30 '11 at 14:44
  • i am not allowed to use friends in the program:( –  Jun 30 '11 at 14:47
  • 1
    So this is homework? Who else would create such a requirement that you *can't* use the language feature explicitly designed to solve the problem that you're having? If this is homework, it's a useless assignment. There's no point in learning the *wrong* way to do something by declaring the *right* way off-limits. – Cody Gray - on strike Jun 30 '11 at 14:49
  • I agree, if this is a homework assignment, it's a pretty bad one. – MGZero Jun 30 '11 at 14:50
  • no its not a homework but i simply i don't want to use friends for some reason –  Jun 30 '11 at 14:52

5 Answers5

3

You can have Display take a dummy const reference to a type that can only be created from a privately nested within the class you want to be able to make the calls. Then in order to pass that type to Display you have to be a member of that class.

But why would you do that when friend does exactly what you want?

Code example:

class AllowedCaller
{
private:
    class FriendHackHelp
    {
    };

public:
    class FriendHack
    {
    public:
        // You can only create a FriendHack from inside this class now...
        FriendHack(const FriendHackHelp&) { }
    };

    void run();
};

class Displayer
{
public:
    void Display(const AllowedCaller::FriendHack&) { /* Whatever */ }
};

void AllowedCaller::run()
{
    Displayer d;
    d.Display(FriendHack(FriendHackHelp()));
}

int main()
{
    return 0;
}
Mark B
  • 95,107
  • 10
  • 109
  • 188
  • thanks for solution . I'll use the friend now. my many (real life one's) friends says not to use friend :) –  Jun 30 '11 at 15:40
2

In C++ this is simply not possible. What you could do is to pass the Neo class as a parameter to the Display() function as a reference and you would have a similar effect.

Constantinius
  • 34,183
  • 8
  • 77
  • 85
0

You can also split your SIM class to 2 classes: Displayable (with Display method) and SIM (with the remaining methods). Then, when creating Neo class, simply do not extend the Displayable class.

bezmax
  • 25,562
  • 10
  • 53
  • 84
0

Let me ask you a question: What exactly is neo? Is it able to be inherited from SIM? If so, make Display() protected as opposed to private.

MGZero
  • 5,812
  • 5
  • 29
  • 46
0

I've been wondering about this too in the past, check these questions:

programming language with granular method and property access

a way in c++ to hide a specific function

However,i come up to terms of the idea that the only way to implement this (in c++ at least) is to create multiple interfaces for each client class, and make each interface a friend of the client class that will access it

so you need to implement all and each of the interfaces with multiple inheritance

Community
  • 1
  • 1
lurscher
  • 25,930
  • 29
  • 122
  • 185
  • Did *you* have any particular reason not to use friends? Or were you thinking of differing degrees of granularity? – Cody Gray - on strike Jun 30 '11 at 15:08
  • yes, different degrees was the goal, which might be achieved with multiple interfaces, in fact, i later recognized that trying to have an interface service multiple clients with different levels of access *screams* for an interface that is trying to do too much and needs to be split (with the inevitable tradeoff ugliness of multiple inheritance, which is not that bad if you are careful though) – lurscher Jun 30 '11 at 15:14