Jump to content
  • Advertisement
Sign in to follow this  
Obbedemus

[C++] Questions about friend-classes and pointers

This topic is 3808 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have some questions about friend-classes and pointers. I have a class A and a friend class B. A contains a pointer to an object B (the friend class). If I have understood it correctly, class B requiers some sort of information on what object it should change, such as a pointer. Therefore, in class B I have a pointer to a class A object which all changes will apply to. So, B is a friend of A, A contains a pointer to an object of B and B contains a pointer to an object of class A. When I try to change any value in class A from class B, I get a reading violation. So, my question is: can class A demand class B to change values in class A, is it this that causes the violation? (I guess it is) If so, is there any workaround? Further, is there any way to make class B change values of class A, without having a pointer to the specific object? I know this might seem a little odd, why can't I just make a member function to A? Well, it's a bit more complicated than what it seems, so no, I can't. Or, I do not want to. I'm quite new to friend classes, so please, have patience with me. I'm still learning. Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Friendship, or lack thereof, will not generate access violations. Friendship relaxes restrictions at compile time. You probably have some other kind of non-kosher pointer mistake going on.

Share this post


Link to post
Share on other sites
I'm new to friend classes myself, but I'll just toss this out there: You say B is a friend of A, but do you mean in A, you put 'friend class B', or in B, you put 'friend class A'?

Friendship doesn't work both ways unless you tell it to. Making B be a friend of A, does not make A a friend of B.

Share this post


Link to post
Share on other sites
Quote:
When I try to change any value in class A from class B


the way you explain it seems like B is a friend class of A. Keep in mind friendship is one way only, so:
class A{
friend class B;
...
}

class B{

...
}

only allows B to access A's private data. Trying to access B's private data from A is not allowed. You would need to give both classes friendship for that.

anyway you'd get compile time errors with improper friendship not access violations like SiCrane mentioned

Share this post


Link to post
Share on other sites
Sorry for the late reply, but every time since I started this thread, the site has been temporarly down for some reason, until now.

I thought that if a pointer told another pointer (that pointed to the same object as the other pointer) to change or read the value of Pointer 1, that might lead to a violation. However, this leads me to a new question.
Both classes will be quite extensive so I want them in diffrent headers with diffrent cpp-files. So it looks something like this:

//Header for class A
#include "B.h"
class A
{
private:
friend class B;

//...
};

//Header for class B
class A;

class B
{
//...
};

//CPP-for B
#include "A.h"


To this there is also #ifndef and other member functions, etc.
Is this the wrong way to do it? I'm very lost and I can't find anything about friend classes and diffrent files.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!