以下为我练手的学生成绩查询代码段,共有两个类,一个是Score类,我想把它设为私有类,另一个为Manager类,Manager为Score类的友元。
Score.h
#include
Score.cpp
#include "StdAfx.h"
#include
#include "Manager.h"
#include "Score.h"
void Score::updateScore(const std::string &pro,double newScore)
{
if(scoreMap.count(pro))
{
scoreMap[pro]=newScore;
}
else
{
std::cout<<"This student's "<
Manager.h
#pragma once
#include
Manager.cpp
#include "StdAfx.h"
#include "Manager.h"
#include
#include
#include
#include
主程序
#include "stdafx.h"
#include "Manager.h"
#include
#include
我是把Manager的数据成员定义成string-Score的Map容器,但是我想把Score设为私有类只有Manager类可访问,这样Score就不能作为容器的元素,设为指针?也不行啊。大神可以指导一下么?
把Score类的声明作为private成员放在Manager类的声明里:
这样你的Manager类里的Score类就是私有的,当别人试图实例化一个Score编译器会报错:
Manager::Score score;//error, class Manager::Score is private
然后每次需要对这个Map进行insert的时候,你用std::make_shared<Score>包上一层传进去。
你把Score类挪到Manager类里面不就完了。