Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Krylloan

"friend", classes and access.

This topic is 5453 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 a class with private data that I want to be able to access publically from within certain files. Using friend for every function in the file that accesses these variables is tedious. Is there any way to specify public access to private variables in a class for an entire file?

Share this post


Link to post
Share on other sites
Advertisement
Whats wrong with just using a public interface for the private variables?


class example
{
public:
const A& getvalue()const{ return value_; }
void setvalue(const A& val)( value_ = val;}
private:
A value_;
};


Allowing a ''file'' (not sure what you mean by that) complete public access to the private members just defeats the whole point of making them private in the first place.

Share this post


Link to post
Share on other sites
Er, the variables are private because there is meant to be no direct access from users of the library. That''s why no public getter/setter functions. (well, there are similar functions, but higher level)

However the library functions themselves need access. The library is spread over multiple files.

Many of the private functions in the library are not (for various reasons) defined inside the class, but there are very few library files which contain the implementation of these functions.

As for a "file", I''m referring to a *.cpp file, a physical file on disk (and it''s associated #includes where necessary) - the typical scope of a .cpp file.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can specify that a class has access to the private members by using ''friend class A;'' somewhere in the class definiton that you want A to have access to.

Share this post


Link to post
Share on other sites
hey ive got the same problem, ap could you give an example? this is what i tried but im still getting the same error.

class Example {
private:
int number;
friend class A;
};

class A
{
public:
void myFunction(Example abc)
{
abc.number=5;
}
};

Share this post


Link to post
Share on other sites
I think friend functions are wonderful and better than using set/get methods.

For example, i have a model object. I want my render object to render it so i pass in the object.

Object3D box;
Render gfx;

gfx.renderModel(box);

class Object3D
{
friend class Render;
private:
float* vertexdata;
float* normals;
//etc
};
class Render
{
void renderModel(Object3D& obj);
};

===============================

void Render::renderModel(Object3D& obj)
{
//i can access all attributes of obj even if they are private
}

In most cases, only the rendering module will need access to vertex data and most other classes should not have acces to it. It makes more sense to make the Render class a friend then to make the variables public and give all classes access to data which they dont require. This is one of the features which i like about C++ which is not found in Java.

Share this post


Link to post
Share on other sites
Er... I''m looking for an ALTERNATE to "friend"... re the title. The class is a friend of class is also being used. It''s global functions that are the problem. (I have to befriend every one and there''s about 60)

They have to be global because they are used with function pointers (and users have to be able to make their own functions that have the same function-pointed type, so I can''t use class-based function pointers.)

Actually, thinking about it, are the function pointers to these types equivalent? (ie. will typecasting one into the other screw up?)

void blah::func();
void func(*blah);





johnnyBravo, your example looks fine to me. Maybe you want to specify that class A exists before example. Try putting this line before Example:

class A;

then define class A after example as u are doing.

I use structs exactly as u are using classes there, and it works fine.





Share this post


Link to post
Share on other sites
namespaces are basically nothing but grouping stuff.

say you have a bunch of functions that calculate stuff you could put them in a namespace called calculaters.
eg
calculaters::sum(3,4)
calculaters:roduct(33,34)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!