如何访问友元类私有属性 [英] How to access to friend class private atributes

查看:84
本文介绍了如何访问友元类私有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我将展示我的代码:

Here i will demonstrate my code:

Curse class:
class Curse{
 private:
  char* name;
  int mark;
 public:
  friend class Student;
  Curse();
   Curse(char*,int);
   ~Curse();
};



诅咒CPP文件:


Curse CPP file:

#include "Curse.h"
#include <string.h>
Curse::Curse(){
strcpy(this->name,"");
this->mark = 0;
}

Curse::Curse(char* name,int mark)
{
strcpy(this->;name,name);
this->mark = mark;
}

Curse::~Curse() { if(name!= NULL) delete[] name;}





--------------- -----------------------------------------



我有学生班:



--------------------------------------------------------

And I have Student class:

#include <string.h>
#include "Curse.h"
class Student{
  private:
    char name[30];
    Curse *curses;
  public:
    Student(){
               strcpy(this->name,"");
                curses = new Curse[10];
                }
};





---------------- ----------



Main.cpp



--------------------------

Main.cpp

#include <iostream>
#include "Student.h"

void main()
{
   Student S1;
   S1.curses[0]->mark = 10; // HERE IS THE EROR 
   // curses[0] is inaccessible ?? PLEASE HELP
}



请帮助,错误在Main.CPP ??



谢谢!


Please help, error is in Main.CPP??

THANKS!

推荐答案

curses 学生<的私有财产/ code> class,因此只能在该类的方法中访问。并且 main 不属于任何类。这与 friend 类无关。请参阅 http://msdn.microsoft.com/en-us/library/4a1hcx0y.aspx [ ^ ]。
curses is a private property of the Student class, so it is only accessible within methods of that class. And main does not belong to any class. This is nothing to do with friend classes. See http://msdn.microsoft.com/en-us/library/4a1hcx0y.aspx[^].


你是否已经诅咒C ++了?我怀疑你的课程名称是course [ ^ ],而不是诅咒 [ ^ ] ;-)



那就是说,朋友应该在C ++中非常谨慎地使用!它是相互依赖类有时必需的关键字,例如容器类(例如std :: vector)及其相应的迭代器。



对于任何其他情况,朋友可以这样说封装 [ ^ ]是你的敌人。但事实并非如此 - 你应该继续学习如何使用它:



输入 accessors and mutators [ ^ ]。而不是调用可能与您的类交互的每个其他类朋友(当您的应用程序是您未编写的程序使用的库时,您甚至无法知道这些内容),提供一个简洁的界面,让其他人以受控的方式访问受保护的成员变量:



Are you already cursing C++? I suspect what you meant to name your class is "course"[^], not "curse"[^] ;-)

That said, friend should be used very sparingly in C++! it is a keyword that is sometimes necessary for strongly interdependend classes, such as container classes (e. g. std::vector) and their corresponding iterators.

For any other case, friend is a way of saying Encapsulation[^] is your enemy. But it isn't - and you should reallly learn how to work with it:

Enter the world of accessors and mutators[^]. Instead of calling every other class that may interact with your class a friend (something you can't even know when your application is a library used by a program you didn't write), provide a neat interface that lets others access your protected member variables in a controlled way:

class Course {
private:
   char* name;
   int mark;
public:
   Course();
   Course(const char* course_name, const int course_mark);
   // accessor definitions:
   const char* get_name() const;
   const int get_mark() const;
   // mutator definitions:
   void set_name(const char* new_name);
   void set_mark(const int new_mark);
};



accessor和mutator函数允许您读取和写入成员变量而无需直接访问它们。这将允许您控制访问并验证类中的更改。例如,如果您的课程已经拥有有效的名称和标记,您可能不希望允许覆盖它。 E. g。用以下单个函数替换上面的两个set函数:


The accessor and mutator functions will allow you to read and write member variables without accessing them directly. This will allow you to control access and verify changes within the class. For example, if your Course already holds a valid name and mark, you may not want to allow overwriting it. E. g. replace the two set functions above with a single one like that:

bool set(const char* course_name, const int new_mark) {
   bool result = true;
   if (name == nullptr) {
      // not set, copy name and mark
      ...
   }
   else if (strcmp(course_name, name) == 0) {
      // ok, correct course chosen, update the mark
      mark = new_mark;
   }
   else {
      // error: trying to change mark on different course
      result = false;
   }
   return result;
}



现在你已经限制了其他人改变你的成员变量的方式而不知道谁会使用这个类。



对于类Student,它类似:如果您希望其他人读取和写入您的成员变量,您应该编写访问器和更改器。请随意不要为每个成员变量提供这些变量,或者只提供阅读的访问者。


Now you have restricted the way others can change your member variables without knowing who will ever use this class.

For the class Student it's similar: you should write accessors and mutators if you want others to read and write your member variables. Feel free not to provide these for every member variable, or maybe only provide accessors for reading.


这篇关于如何访问友元类私有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆