如何从其他结构访问受保护的结构变量 所以我正在编写一个C++程序,它将模拟图书馆卡目录。我为卡片和每张卡片上的所有信息定义了一个struct,以及一个工作vector和iterator,以使用全局无效函数访问/打印指定卡片上的所有变量
现在,我想在一个新定义的如何从其他结构访问受保护的结构变量 所以我正在编写一个C++程序,它将模拟图书馆卡目录。我为卡片和每张卡片上的所有信息定义了一个struct,以及一个工作vector和iterator,以使用全局无效函数访问/打印指定卡片上的所有变量,c++,vector,struct,protected,C++,Vector,Struct,Protected,现在,我想在一个新定义的struct目录中移动这个void函数,这个目录处理所有处理图书卡的方法,比如insert/push\u back,search或remove/擦除/弹出式返回。我还希望卡片下的变量得到保护,因为我经常被告知,将类/结构变量设置为私有(我对继承的其他类进行了保护) /#包括 #包括 //#包括 #包括 #包括 使用名称空间std; 结构卡 { 公众: 卡片(字符串标题、字符串名称) { 这->标题=标题; 此->名称=名称; } //受保护: 字符串title=“未知”;
struct
目录中移动这个void函数,这个目录处理所有处理图书卡的方法,比如insert
/push\u back
,search
或remove
/擦除
/弹出式返回
。我还希望卡片下的变量得到保护
,因为我经常被告知,将类/结构变量设置为私有
(我对继承的其他类进行了保护
)
/#包括
#包括
//#包括
#包括
#包括
使用名称空间std;
结构卡
{
公众:
卡片(字符串标题、字符串名称)
{
这->标题=标题;
此->名称=名称;
}
//受保护:
字符串title=“未知”;
string name=“未知”;
};
病媒试验;
向量::迭代器;
无效显示卡(矢量测试)
{
for(it=test.begin();it!=test.end();it++)
{
如果(它->标题!=“未知”)
{
printf(“%s\n”,it->title.c_str());
printf(“%s\n”,it->name.c_str());
}
}
}
int main()
{
卡片册1={“石南丛中的蛇/凯·凯尼恩”,“凯·凯尼恩”};
卡片簿2={“第二次世界大战以来的美国和中东/
T.G.Fraser.,“T.G.Fraser”};
卡片簿3={“我的马和沃利”,“杰森·韦伯”};
测试。推回(第1册);
测试。推回(第2册);
测试。推回(第三册);
展示卡(测试);
getchar();
返回0;
}
我想我的问题是,我如何从main调用Catalog struct,然后访问Card下的受保护变量,以便打印受保护的变量
不能像在目录中列出朋友结构卡那样简单,是吗
编辑:我到处玩,发现卡片下的friend struct Catalog
能够消除它试图访问的受保护变量的void函数中的错误。尽管我将main
中的所有对象定义为Card
,但我仍在努力创建主传递目录
我想我可以尝试一个在main中调用的setCard()
,它在Catalog中定义,在Catalog中使用vector来引用受保护的变量 @obidyne,欢迎来到StackOverflow。如果您的目标是保护成员,但仍然能够显示它们(作为格式化字符串),则可以实现公共方法showCard
,重命名其他函数showCards
,并为向量的每个对象调用公共方法
仅举一个例子(使用您自己的代码):
/#包括
#包括
//#包括
#包括
#包括
使用名称空间std;
结构卡
{
公众:
卡片(字符串标题、字符串名称)
{
这->标题=标题;
此->名称=名称;
}
作废展示卡()
{
如果(此->标题!=“未知”)
{
printf(“%s\n”,this->title.c_str());
printf(“%s\n”,this->name.c_str());
}
}
受保护的:
字符串title=“未知”;
string name=“未知”;
};
病媒试验;
向量::迭代器;
无效显示卡(矢量测试)
{
for(it=test.begin();it!=test.end();it++)
{
it->showCard();
}
}
int main()
{
卡片册1={“石南丛中的蛇/凯·凯尼恩”,“凯·凯尼恩”};
卡片簿2={“第二次世界大战以来的美国和中东/
T.G.Fraser.,“T.G.Fraser”};
卡片簿3={“我的马和沃利”,“杰森·韦伯”};
测试。推回(第1册);
测试。推回(第2册);
测试。推回(第三册);
展示卡(测试);
getchar();
返回0;
}
@obidyne,欢迎来到StackOverflow。如果您的目标是保护成员,但仍然能够显示它们(作为格式化字符串),则可以实现公共方法showCard
,重命名其他函数showCards
,并为向量的每个对象调用公共方法
仅举一个例子(使用您自己的代码):
/#包括
#包括
//#包括
#包括
#包括
使用名称空间std;
结构卡
{
公众:
卡片(字符串标题、字符串名称)
{
这->标题=标题;
此->名称=名称;
}
作废展示卡()
{
如果(此->标题!=“未知”)
{
printf(“%s\n”,this->title.c_str());
printf(“%s\n”,this->name.c_str());
}
}
受保护的:
字符串title=“未知”;
string name=“未知”;
};
病媒试验;
向量::迭代器;
无效显示卡(矢量测试)
{
for(it=test.begin();it!=test.end();it++)
{
it->showCard();
}
}
int main()
{
卡片册1={“石南丛中的蛇/凯·凯尼恩”,“凯·凯尼恩”};
卡片簿2={“第二次世界大战以来的美国和中东/
T.G.Fraser.,“T.G.Fraser”};
卡片簿3={“我的马和沃利”,“杰森·韦伯”};
测试。推回(第1册);
测试。推回(第2册);
测试。推回(第三册);
展示卡(测试);
getchar();
返回0;
}
有多种方法,正确的方法取决于上下文。以下是一些可能的解决方案,从最简单/最黑客的到最详细/最难的(不是详尽的列表):
1.把一切公之于众
3.为卡创建get
ters和set
ters
。每次将成员设置为私有/受保护时,都会为其提供get\u成员
和set\u成员
方法
这样每个人都可以访问该成员,但是,只有使用这些方法,他们才能访问该成员。您甚至可以为不存在的成员创建getter/setter(即在需要时计算它们)
因为代码是spe
//#include <cstdio>
#include <iostream>
//#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
struct Card
{
public:
Card(string title, string name)
{
this->title = title;
this->name = name;
}
//protected:
string title = "Unknown";
string name = "Unknown";
};
vector<Card> test;
vector<Card>::iterator it;
void showCard(vector<Card> test)
{
for (it = test.begin(); it != test.end(); it++)
{
if (it->title != "Unknown")
{
printf("%s\n", it->title.c_str());
printf("%s\n", it->name.c_str());
}
}
}
int main()
{
Card book1 = { "Serpent in the heather / Kay Kenyon", "Kay Kenyon"};
Card book2 = { "USA and the Middle East since World War 2 /
T.G. Fraser.", "T.G. Fraser"};
Card book3 = { "My Horse and wally", "Jason Weber" };
test.push_back(book1);
test.push_back(book2);
test.push_back(book3);
showCard(test);
getchar();
return 0;
}
//#include <cstdio>
#include <iostream>
//#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
struct Card
{
public:
Card(string title, string name)
{
this->title = title;
this->name = name;
}
void showCard()
{
if (this->title != "Unknown")
{
printf("%s\n", this->title.c_str());
printf("%s\n", this->name.c_str());
}
}
protected:
string title = "Unknown";
string name = "Unknown";
};
vector<Card> test;
vector<Card>::iterator it;
void showCards(vector<Card> test)
{
for (it = test.begin(); it != test.end(); it++)
{
it->showCard();
}
}
int main()
{
Card book1 = { "Serpent in the heather / Kay Kenyon", "Kay Kenyon"};
Card book2 = { "USA and the Middle East since World War 2 /
T.G. Fraser.", "T.G. Fraser"};
Card book3 = { "My Horse and wally", "Jason Weber" };
test.push_back(book1);
test.push_back(book2);
test.push_back(book3);
showCards(test);
getchar();
return 0;
}
...
struct Card{
public:
Card(string title, string name){
this->title = title;
this->name = name;
}
string title = "Unknown";
string name = "Unknown";
};
...
void showCard(vector<Card> test){
for (it = test.begin(); it != test.end(); it++){
if (it->title != "Unknown"){
printf("%s\n", it->title.c_str());
printf("%s\n", it->name.c_str());
}
}
}
...
struct Card;
void showCard(vector<Card> test);
struct Card{
public:
friend void showCard(vector<Card> test);
Card(string title, string name){
this->title = title;
this->name = name;
}
protected:
string title = "Unknown";
string name = "Unknown";
};
...
void showCard(vector<Card> test){
for (it = test.begin(); it != test.end(); it++){
if (it->title != "Unknown"){
printf("%s\n", it->title.c_str());
printf("%s\n", it->name.c_str());
}
}
}
...
struct Card{
protected:
string title = "Unknown";
string name = "Unknown";
public:
Card(string title, string name){
this->title = title;
this->name = name;
}
string get_title(){
return this->title;
}
void set_title(string new_title){
this->title = new_title;
}
string get_name(){
return this->name;
}
void set_name(string new_name){
this->name = new_name;
}
};
...
void showCard(vector<Card> test){
for (it = test.begin(); it != test.end(); it++){
if (it->get_title() != "Unknown"){
printf("%s\n", it->get_title().c_str());
printf("%s\n", it->get_name().c_str());
}
}
}
const string& get_title() const {
return this->title;
}
void set_title(const string& new_title){
this->title = new_title;
}
#include <string>
#include <algorithm>
#include <iterator>
string get_title_and_name(){
// Concatenates the title and name
return this->title + " / " + this->name;
}
void set_title_and_name(string new_string){
// Splits the string between a title and a name
std::size_t split_point = 0;
split_point = new_string.find('/');
this->title = new_string.substr(0, split_point);
// We don't want to include the char '/' of
// the new_string in this->name, so use
// (split_point + 1) instead of split_point
this->name = new_string.substr(split_point + 1, new_string.size() - (split_point + 1));
}
//#include <cstdio>
#include <iostream>
//#include <stdio.h>
#include <vector>
#include <string>
using namespace std;
// As in solution 3
struct Card {
protected:
string title = "Unknown";
string name = "Unknown";
public:
Card(string title, string name){
this->title = title;
this->name = name;
}
// Right now we only need getters,
// but we could have setters as well
// (the names are in camelCase to follow
// showCard() naming convention)
string getTitle(){
return this->title;
}
string getName(){
return this->name;
}
};
struct Catalog {
protected:
// This one was a global variable previously
// Also we don't specify a default value
// for it here, we will do that in the constructor
vector<Card> test;
public:
Catalog(){
// The start value of test will be a empty vector
this->test = vector<Card>();
}
// We moved void showCard(vector<Card> test) to here
void showCard(){
// This is a local variable now
vector<Card>::iterator it;
// For loop as in solution 3
for (it = this->test.begin(); it != this->test.end(); it++){
if (it->getTitle() != "Unknown"){
printf("%s\n", it->getTitle().c_str());
printf("%s\n", it->getName().c_str());
}
}
}
// A new method for adding cards,
// because external code shouldn't care
// about how we add or remove card or even
// if we store cards in this machine or in a web server
void addCard(Card card){
this->test.push_back(card);
}
};
int main()
{
Card book1 = { "Serpent in the heather / Kay Kenyon", "Kay Kenyon"};
Card book2 = { "USA and the Middle East since World War 2 / T.G. Fraser.", "T.G. Fraser"};
Card book3 = { "My Horse and wally", "Jason Weber" };
Catalog catalog;
catalog.addCard(book1);
catalog.addCard(book2);
catalog.addCard(book3);
// We could even do something like
// catalog.addCard({ "My Horse and wally", "Jason Weber" });
// thankfully to the new addCard method.
// We wouldn't even need to declare book1, book2 and book3
// if we used it that way
catalog.showCard();
getchar();
return 0;
}