473,238 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 software developers and data experts.

Friend-Based Interfaces

I just thought I'd put this out here to get others' opinions on the
concept. By no means do I claim to be a C++ guru (I'm really new, to
be honest), but it just struck me as a pretty neat thing when it
occurred to me.
----------

#include <iostream>
// Forward declarations
class FirstUser;
class SecondUser;
// The interfaces to be used.
class FirstInterface {
friend class FirstUser;
private:
virtual void runFirst() = 0;
};

class SecondInterface {
friend class SecondUser;
private:
virtual void runSecond() = 0;
};
// The users of those interfaces
class FirstUser {
public:
void function(FirstInterface& foo) {
foo.runFirst();
}
};

class SecondUser {
public:
void function(SecondInterface& foo) {
foo.runSecond();
}
};
// The utilizer of those interfaces
class FinalInterface : public FirstInterface, public SecondInterface {
public:
FinalInterface()
: totalCount_(0)
{}

int totalCount() {
return(totalCount_);
}
private:
// These are only accessible from classes with the proper
permissions
void runFirst() {
std::cout << "First function run" << std::endl;
++totalCount_;
}

void runSecond() {
std::cout << "Second function run" << std::endl;
++totalCount_;
}
int totalCount_;
};

int main() {
FirstUser first;
SecondUser second;

FinalInterface final;
first.function(final);
second.function(final);

std::cout << final.totalCount() << std::endl;

return(0);
}
----------

To me it seems that a setup like this would increase the time to put
together a class (if only marginally to divide up otherwise public
functions by purpose/users), but any sort of debugging would be made
significantly easier - if value X is off somewhere, the list of what
can modify it is reduced from "Everywhere that knows this class exists
and can use its public members" to "This list of classes". It would
also encourage programmers to not take the lazy way out of problems
since they don't have access to every function at every point ("I
could just put a call here to add one to that, but then I'd have to
mess up permissions...").
Overall, I see it as a stronger encapsulation, where not only can you
pinpoint what's changing private data, but what's asking for it to be
changed in the first place. Instead of trying to pinpoint a the user
of a certain member function, you can spend more time pinpointing what
that user did.
Again though, I *am* rather new at this, so I'd appreciate some
comments and feedback. Am I completely off base on this one, and if
so, how? :)

May 30 '07 #1
0 982

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external...
15
by: Samee Zahur | last post by:
Question: How do friend functions and static member functions differ in terms of functionality? I mean, neither necessarily needs an object of the class to be created before they are called and...
7
by: Steve | last post by:
I'm just curious, why did give VB.Net a Friend keyword but not C#?
2
by: K. Shier | last post by:
Friend variables "are accessible from within the program that contains their declaration and from anywhere else in the same assembly." according to the VS.Net help. This sounds like a pretty broad...
9
by: Adam Badura | last post by:
I have code like this template<int size> big_int { /* ... */ template<int size2> friend class big_int<size2>; }; What I wanted to achive is to be able to easly convert different sized...
2
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
5
by: Steven T. Hatton | last post by:
This note appears in the discussion of name hiding and uniqueness: §3.3 #4 This note is item #6 in the discussion of "Point of declaration" §3.3.1 #6 What exactly do these statements mean?...
3
by: Filimon Roukoutakis | last post by:
Dear all, I have the following concept name { space1 { class Friend { };
5
by: tuananh87vn | last post by:
hi, I'mm writing script for adding friend to a friend list.i'm using a form including friend_name (text), message (textarea) and below is my code: if(isset($_POST)) { ...
6
by: WaterWalk | last post by:
I find friend declaration just very tricky. I tried the following examples on both MingW(gcc 3.4.2) and VC++ 2005. The results are surprising. Example1: namespace ns1 { class Test { friend...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.