Suppose a class: Library
假设一个类:Library
And we have a group of derived class from the base class LibraryCustomer, such as: Kid, Parent, Student, etc
我们有一组来自基类LibraryCustomer的派生类,例如:Kid,Parent,Student等
In the class of Library, there are a group of (tons of) private members variables. Since there are tons of private members in the class of Library, I do not want to use getter and setter which are tedious. Plus the LibraryCustomer derived classes will often refer to those members. Getter and Setter are not convenient.
在图书馆类中,有一组(吨)私有成员变量。由于图书馆类中有大量的私人成员,我不想使用繁琐的getter和setter。此外,LibraryCustomer派生类通常会引用这些成员。 Getter和Setter不方便。
In order for those LibraryCustomers to access those private member in Library, I need to claim those LibraryCustomers as friend classes in Library.
为了让那些LibraryCustomers访问Library中的私有成员,我需要将这些LibraryCustomers声明为Library中的朋友类。
But since the derived classes keep growing, I do not want to add them one by one in the class Library.
但是由于派生类不断增长,我不想在类库中逐个添加它们。
Add the base class LibraryCustomer as friend in Library seems not to work. So what is another better way?
添加基类LibraryCustomer作为库中的朋友似乎无法正常工作。那么另一种更好的方法是什么?
[Update] I want to access tons of private member variables in the class of Library. Since there are many, so I do not want to use getter and setter. I hope the derived classes from LibraryCustomer can freely access those private member variables in the class of Library.
[更新]我想访问Library类中的大量私有成员变量。由于有很多,所以我不想使用getter和setter。我希望LibraryCustomer的派生类可以*访问Library类中的私有成员变量。
1 个解决方案
#1
Provide a function in LibraryCustomer
that accesses Library
to get the data and provides that data to the classes derived from LibraryCustomer
.
在LibraryCustomer中提供一个函数,该函数访问Library以获取数据并将该数据提供给从LibraryCustomer派生的类。
class Library
{
friend class LibraryCustomer;
private:
std::string name;
};
class LibraryCustomer
{
protected:
std::string getLibraryName(Library const& lib)
{
return lib.name;
}
};
class Kid : public LibraryCustomer
{
// Can use LibraryCustomer::getLibraryName() any where
// it needs to.
};
Having said that, it will be easier to provide access to the data from Library
itself.
话虽如此,提供对图书馆本身数据的访问会更容易。
class Library
{
public:
std::string getName() const { return name; }
private:
std::string name;
};
Then, there won't be the need for the friend
declaration and the wrapper function LibraryCustomer::getLibraryName()
.
然后,不需要friend声明和包装函数LibraryCustomer :: getLibraryName()。
EDIT
@MooingDuck has interesting suggestion. If you have to expose many such variables, it might be better to put them all in one class. Working code at http://coliru.stacked-crooked.com/a/2d647c3d290604e9.
@MooingDuck有一个有趣的建议。如果必须公开许多这样的变量,最好将它们全部放在一个类中。 http://coliru.stacked-crooked.com/a/2d647c3d290604e9上的工作代码。
#include <iostream>
#include <string>
class LibraryInterface {
public:
std::string name;
std::string name1;
std::string name2;
std::string name3;
std::string name4;
std::string name5;
std::string name6;
};
class Library : private LibraryInterface
{
public:
Library() {name="BOB";}
private:
LibraryInterface* getLibraryInterface() {return this;} //only LibraryCustomer can aquire the interface pointer
friend class LibraryCustomer;
};
class LibraryCustomer
{
protected:
LibraryInterface* getLibraryInterface(Library& lib) {return lib.getLibraryInterface();} //only things deriving from LibraryCustomer can aquire the interface pointer
};
class Kid : public LibraryCustomer
{
public:
void function(Library& lib) {
LibraryInterface* interface = getLibraryInterface(lib);
std::cout << interface->name;
}
};
int main() {
Library lib;
Kid k;
k.function(lib);
}
#1
Provide a function in LibraryCustomer
that accesses Library
to get the data and provides that data to the classes derived from LibraryCustomer
.
在LibraryCustomer中提供一个函数,该函数访问Library以获取数据并将该数据提供给从LibraryCustomer派生的类。
class Library
{
friend class LibraryCustomer;
private:
std::string name;
};
class LibraryCustomer
{
protected:
std::string getLibraryName(Library const& lib)
{
return lib.name;
}
};
class Kid : public LibraryCustomer
{
// Can use LibraryCustomer::getLibraryName() any where
// it needs to.
};
Having said that, it will be easier to provide access to the data from Library
itself.
话虽如此,提供对图书馆本身数据的访问会更容易。
class Library
{
public:
std::string getName() const { return name; }
private:
std::string name;
};
Then, there won't be the need for the friend
declaration and the wrapper function LibraryCustomer::getLibraryName()
.
然后,不需要friend声明和包装函数LibraryCustomer :: getLibraryName()。
EDIT
@MooingDuck has interesting suggestion. If you have to expose many such variables, it might be better to put them all in one class. Working code at http://coliru.stacked-crooked.com/a/2d647c3d290604e9.
@MooingDuck有一个有趣的建议。如果必须公开许多这样的变量,最好将它们全部放在一个类中。 http://coliru.stacked-crooked.com/a/2d647c3d290604e9上的工作代码。
#include <iostream>
#include <string>
class LibraryInterface {
public:
std::string name;
std::string name1;
std::string name2;
std::string name3;
std::string name4;
std::string name5;
std::string name6;
};
class Library : private LibraryInterface
{
public:
Library() {name="BOB";}
private:
LibraryInterface* getLibraryInterface() {return this;} //only LibraryCustomer can aquire the interface pointer
friend class LibraryCustomer;
};
class LibraryCustomer
{
protected:
LibraryInterface* getLibraryInterface(Library& lib) {return lib.getLibraryInterface();} //only things deriving from LibraryCustomer can aquire the interface pointer
};
class Kid : public LibraryCustomer
{
public:
void function(Library& lib) {
LibraryInterface* interface = getLibraryInterface(lib);
std::cout << interface->name;
}
};
int main() {
Library lib;
Kid k;
k.function(lib);
}