472,365 Members | 1,234 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to declare friendship-ness to all inherited class?

Hello,

struct S{
friend class B;
private:
int i;
};

class B{
B(){}
virtual ~B(){}
};

class D : public B{
};

it turns out that although for D, its parent class B is a friend of S, D is
not a friend to S, therefore
it is allowed to manipulate private member i of S within D. Should i declare
friend class all one by one in S
or is there a succinct way of expressing that all the inherited class of B
should be friend of S?

br,Kai
Jul 23 '05 #1
2 1483
Sorry to bother, read the FAQ, it looks it is not possible by rules.

"Kai Wu" <ka******@nokia.com> wrote in message
news:uA****************@news2.nokia.com...
Hello,

struct S{
friend class B;
private:
int i;
};

class B{
B(){}
virtual ~B(){}
};

class D : public B{
};

it turns out that although for D, its parent class B is a friend of S, D is not a friend to S, therefore
it is allowed to manipulate private member i of S within D. Should i declare friend class all one by one in S
or is there a succinct way of expressing that all the inherited class of B
should be friend of S?

br,Kai

Jul 23 '05 #2
--- Kai Wu wrote:
Should i declare friend class all one by one in S
or is there a succinct way of expressing that all
the inherited class of B should be friend of S?


The second idea is impossbile. The first is generally
unacceptable, since it does not provide for future
derived classes.

So try it a different way. Give B a new member function
that returns a reference to the appropriate member of S.
Declare this new member function "protected". Then all
derived classes of B, even those you don't anticipate,
can access this data member of S through the function.

struct S {
friend class B;

private:
int i;
};
class B {
B(){}
virtual ~B(){}

protected:
int & geti(struct S & ss) {
return ss.i;
}
const int & geti(const struct S & ss) {
return ss.i;
}
};

Jul 23 '05 #3

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

Similar topics

7
by: Erik | last post by:
private and protected members can only be accessed by deriving a class or by giving friendship. The problem with friendship is that friend statements are written inside the class. A FriendShip...
5
by: Jiggaz | last post by:
Hi, Look my stored procedure : __________________ ALTER PROCEDURE dbo.CreateAccount @Nickname varchar(30), @Password varchar(15), @Email varchar(50), @Date datetime,
2
by: Lance Geeck | last post by:
I have many items that I lifted off from Microsoft's website several years ago. These samples were in VB6. I now want to convert an application to VB.NET. I am getting an error that says "As Any...
1
by: marfi95 | last post by:
I'm trying to call methods in a C++ dll from vb.net that is not a .com component. I've read I can just use regular Declare statements just as you do in VB6 except that some of the types need to...
5
by: Mike Gleason jr Couturier | last post by:
Hi, I would like class B to access private members of class A for optimisation reasons... To be specific. B instanciates A and should initializes A's members to some values. But the public...
4
by: werasm | last post by:
I've read the following somewhere: "Friendship is the strongest form of coupling there is. That is, you introduce a high degree of dependency when you declare a friend, since the friend becomes...
1
by: okonita | last post by:
Hi all, My environment is DB2 UDBv8.2 on Windows and Linux. I am getting a lot of errors on the script you see below. Some of the error I am geting are: ...
0
by: Hanshika Sathey | last post by:
SUMMER DATING + FRIENDSHIP WITH ME..! Hii.... I'm Hanshika Sathey, from Mumbai.. Make Me your Best Friend Simple join to my Best Friends List.. ...
6
by: Hicham Mouline | last post by:
Hello, A semi-skeptical colleague is asking me why friendship is not inheritable, specifically: class Base { public: virtual void f() const =0; }; class Derived : public Base {
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

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.