using friend
-
Visual Studio 2008, c++ Class C_Messages works with various structures called messages. The initial configuration of the messages is by reading a text file and is rather extensive. C_Messages instantiates a class called C_Configuration to read the text file and perform all the startup work. Currently C_Messages instantiates C_Configuration then calls some functions handing C_Configuration pointers to the structures. Is there some way that C_Configuration can be declare a friend of C_Messages and dispense with the functions just to set the pointers. I am thinking of something like this in C_Messages.h
Class C_Configuration;
Class C_Messages
{
…
Friend C_Configuration;
int x;
}Then in file C_Configuration.cpp would be something like:
Class C_Messages
Int Do_This()
{
X = 2;
}When I read the various forum questions on this none seem to apply to a simple relationship like this. Maybe its just not possible so I am trying make a simple question out of this. EDIT I just figured out part of it and why the compiler is saying the variable is not static. If I don't want static variables can I do something like hand over the this pointer to C_Messages to C_Configuration then C_Configuration can access the members of C_Messages?
Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com
-
Visual Studio 2008, c++ Class C_Messages works with various structures called messages. The initial configuration of the messages is by reading a text file and is rather extensive. C_Messages instantiates a class called C_Configuration to read the text file and perform all the startup work. Currently C_Messages instantiates C_Configuration then calls some functions handing C_Configuration pointers to the structures. Is there some way that C_Configuration can be declare a friend of C_Messages and dispense with the functions just to set the pointers. I am thinking of something like this in C_Messages.h
Class C_Configuration;
Class C_Messages
{
…
Friend C_Configuration;
int x;
}Then in file C_Configuration.cpp would be something like:
Class C_Messages
Int Do_This()
{
X = 2;
}When I read the various forum questions on this none seem to apply to a simple relationship like this. Maybe its just not possible so I am trying make a simple question out of this. EDIT I just figured out part of it and why the compiler is saying the variable is not static. If I don't want static variables can I do something like hand over the this pointer to C_Messages to C_Configuration then C_Configuration can access the members of C_Messages?
Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com
What you have shown above doesn't really make sense. The use of the
friend
keyword is to allow access to protected or private variables of the other class, but you still need an instance of the class to access them. It's most useful when the friend class inherits the other one. If you are just interested in accessing the properties or methods of an object of another class, then you should make them public. -
What you have shown above doesn't really make sense. The use of the
friend
keyword is to allow access to protected or private variables of the other class, but you still need an instance of the class to access them. It's most useful when the friend class inherits the other one. If you are just interested in accessing the properties or methods of an object of another class, then you should make them public.Re:
Quote:
The use of the friend keyword is to allow access to protected or private variables of the other class
That is the part I thought I understood.
Quote:
but you still need an instance of the class to access them.
That is what I have now figured out, but I think only partially.
Quote:
It's most useful when the friend class inherits the other one
Is the intent most useful or only useful. I am thinking only. I was thinking friend had a rather broader scope. Class M starts running and creates class C to do a bunch of startup work. C is owned by M but does not inherit from M. C is deleted immediately upon completing its work. Other than for startup (which requires some 80,000+ data items from a file), nothing in M needs to be public. But this entire app is and always be run in a closed system with strictly controlled ins and outs. I am leery of public variables but that would probably better than sending over a flock of pointers. Does the use of public seem to be my best option.
Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com
-
Re:
Quote:
The use of the friend keyword is to allow access to protected or private variables of the other class
That is the part I thought I understood.
Quote:
but you still need an instance of the class to access them.
That is what I have now figured out, but I think only partially.
Quote:
It's most useful when the friend class inherits the other one
Is the intent most useful or only useful. I am thinking only. I was thinking friend had a rather broader scope. Class M starts running and creates class C to do a bunch of startup work. C is owned by M but does not inherit from M. C is deleted immediately upon completing its work. Other than for startup (which requires some 80,000+ data items from a file), nothing in M needs to be public. But this entire app is and always be run in a closed system with strictly controlled ins and outs. I am leery of public variables but that would probably better than sending over a flock of pointers. Does the use of public seem to be my best option.
Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com
bkelly13 wrote:
Class M starts running and creates class C to do a bunch of startup work. C is owned by M but does not inherit from M. C is deleted immediately upon completing its work.
Then there is no need for them to be friends.
bkelly13 wrote:
I am leery of public variables
You can hid the variables by making them private, and creating public methods to get or set them, which is the generally recommended pattern in OOP. At the end of the day it all depends on what each of your classes is supposed to do.
-
Re:
Quote:
The use of the friend keyword is to allow access to protected or private variables of the other class
That is the part I thought I understood.
Quote:
but you still need an instance of the class to access them.
That is what I have now figured out, but I think only partially.
Quote:
It's most useful when the friend class inherits the other one
Is the intent most useful or only useful. I am thinking only. I was thinking friend had a rather broader scope. Class M starts running and creates class C to do a bunch of startup work. C is owned by M but does not inherit from M. C is deleted immediately upon completing its work. Other than for startup (which requires some 80,000+ data items from a file), nothing in M needs to be public. But this entire app is and always be run in a closed system with strictly controlled ins and outs. I am leery of public variables but that would probably better than sending over a flock of pointers. Does the use of public seem to be my best option.
Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com
If all instances of C are owned by and created by an instance of M and there are no other clients of C then I can't see a lot of problem in making M a friend of C. You're essentially saying that C is a part of M you don't need all the time so you want to tear it off and discard it after some initialisation process. Another way of doing it would be to introduce a new class, A that contains the bits of M that C needs to manipulate. M would contain an instance of A and pass a pointer to the instance to C when needed. That gives you a bit more future proofing and decoupling - both C and M don't depend on each other and just depend on A. Yet another way would be to have a getter/setter interface on M but if C's the only thing that would want to use it then you're complicating the interface of M for no real reason. Personally I'd go for the first and consider the second or third later if the requirements change and you need more flexibility.
-
If all instances of C are owned by and created by an instance of M and there are no other clients of C then I can't see a lot of problem in making M a friend of C. You're essentially saying that C is a part of M you don't need all the time so you want to tear it off and discard it after some initialisation process. Another way of doing it would be to introduce a new class, A that contains the bits of M that C needs to manipulate. M would contain an instance of A and pass a pointer to the instance to C when needed. That gives you a bit more future proofing and decoupling - both C and M don't depend on each other and just depend on A. Yet another way would be to have a getter/setter interface on M but if C's the only thing that would want to use it then you're complicating the interface of M for no real reason. Personally I'd go for the first and consider the second or third later if the requirements change and you need more flexibility.
First option was:
Quote:
then I can't see a lot of problem in making M a friend of C.
I was thinking that C would be a friend of M allowing C to see into M. Am I thinking wrong on that. What is the syntax to make this declaration? C gets access to the variables of M, can change them, then C gets deleted.
Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com
-
First option was:
Quote:
then I can't see a lot of problem in making M a friend of C.
I was thinking that C would be a friend of M allowing C to see into M. Am I thinking wrong on that. What is the syntax to make this declaration? C gets access to the variables of M, can change them, then C gets deleted.
Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com
-
Visual Studio 2008, c++ Class C_Messages works with various structures called messages. The initial configuration of the messages is by reading a text file and is rather extensive. C_Messages instantiates a class called C_Configuration to read the text file and perform all the startup work. Currently C_Messages instantiates C_Configuration then calls some functions handing C_Configuration pointers to the structures. Is there some way that C_Configuration can be declare a friend of C_Messages and dispense with the functions just to set the pointers. I am thinking of something like this in C_Messages.h
Class C_Configuration;
Class C_Messages
{
…
Friend C_Configuration;
int x;
}Then in file C_Configuration.cpp would be something like:
Class C_Messages
Int Do_This()
{
X = 2;
}When I read the various forum questions on this none seem to apply to a simple relationship like this. Maybe its just not possible so I am trying make a simple question out of this. EDIT I just figured out part of it and why the compiler is saying the variable is not static. If I don't want static variables can I do something like hand over the this pointer to C_Messages to C_Configuration then C_Configuration can access the members of C_Messages?
Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com
friend can be used to give another class access to the protected and private members of the class that specified friend. If C_Messages want to access the protected and private members of the class C_Configuration then C_Configuration specified C_Messages as friend. If you aggregate the class C_Messages so that is has a C_Configuration, you can only access the public members of C_Configuration in C_Messages. But with the friend specifier you can also access the protected and private members. Sample code:
class C2;
class C1
{
public:
int public_var;
protected:
int protected_var;
private:
int private_var;
friend class C2;
};class C2
{
public:
class C1 member;
void set_private_var (int v) { member.private_var = v; }
void set_protected_var (int v) { member.protected_var = v; }
};void Test()
{
C2 obj;obj.member.public\_var = 100; // ok // ERROR: can't access protected and private member // obj.member.protected\_var = 100; // obj.member.private\_var = 200; obj.set\_protected\_var(100); // ok obj.set\_private\_var(200); // ok
}
Another solution is working with an intermediate class which specifies the friend. In this case you get only access to public and protected members of the base class. Sample code:
class C1
{
public:
int public_var;
protected:
int protected_var;
private:
int private_var;
};class C1B : public C1
{
friend C2;
}class C2
{
public:
class C1B member;
void set_protected_var (int v) { member.protected_var = v; }
};void Test()
{
C2 obj;obj.member.public\_var = 100; // ok // ERROR: can't access protected and private member // obj.member.protected\_var = 100; // obj.member.private\_var = 200; obj.set\_protected\_var(100); // ok
}