Making classes public to other classes in C++ -


if have 2 classes example follows:

class {...}  class b {...} 

if want make class a public class b, make members of class a public or can use public class {...}?

is there way tell class b example class a public you? in other words, can make public classes a protected or private others? or, matter of deriving class (inheritance)?

thanks.

there's substantial difference between making class public , making contents public.

if define class in include file (.h file) making class public. every other source file includes include file know class, , can e.g. have pointer it.

the way make class private, put definition in source (.cpp) file.

even when make class public, don't have make contents of class public. following example extreme one:

class myclass    {    private:       myclass();       ~myclass();       void setvalue(int i);       int getvalue() const;    }; 

if definition put in include file, every other source can refer (have pointer to) class, since methods in class private, no other source may construct it, destruct it, set value or value.

you make contents of class public putting methods in 'public' part of class definition, this:

class myclass    {    public:       myclass();       ~myclass();       int getvalue() const;    private:       void setvalue(int i);    }; 

now may construct , destruct instances of class, , may value. setting value however, not public, nobody able set value (except class itself).

if want make class public other class of application, not complete application, should declare other class friend, e.g.:

class someotherclass; class myclass    {    friend someotherclass;    public:       myclass();       ~myclass();       int getvalue() const;    private:       void setvalue(int i);    }; 

now, someotherclass may access private methods myclass, may call setvalue set value of myclass. other classes still limited public methods.

unfortunately, there no way in c++ make part of class public limited set of other classes. so, if make class friend, able access private methods. therefore, limit number of friends.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -