私人类成员未完全封装? [英] Private class members not fully encapsulated?

查看:178
本文介绍了私人类成员未完全封装?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是使用C ++的实例层级封装的后续文章。 / p>

我定义了一个类,并从该类创建了两个对象。

  #include< iostream> 
#include< ctime>
#include< string>

using namespace std;

class timeclass {
private:
string date;

time_t gmrawtime,rawtime;
struct tm * timeinfo;
char file_date [9];

void tm_init(int);

public:
timeclass(int);
void print_date();
};

void timeclass :: tm_init(int y){
timeinfo = gmtime(& rawtime);
timeinfo-> tm_year = y-1900; // timeinfo-> tm_year保存自1900年以来的年数
timeinfo-> tm_mon = 0;
timeinfo-> tm_mday = 1;
timeinfo-> tm_hour = 0;
timeinfo-> tm_min = 0;
timeinfo-> tm_sec = 0;
}

timeclass :: timeclass(int y){
timeclass :: tm_init(y)
gmrawtime = mktime(timeinfo) - timezone;
}

void timeclass :: print_date(){
strftime(file_date,9,%Y%m%d,timeinfo);

date = string(file_date);
cout<<< date<< endl;
}

/ * ----------------------------------- ------------------------------------ * /

int main()
{
timeclass time1(1991);
timeclass time2(1992);

time1.print_date(); //打印19920101,尽管使用1991初始化
time2.print_date(); // Prints 19920101,as expected

return 0;
}

这个例子是从我的主程序切片和切片的日期计数器的一部分,但它说明了我的观点。我想有一个日期计数器运行每个类的实例(time1和time2),但它看起来像一旦我构造time2对象,'timeinfo'变量,我认为被封装在time1被time2构造函数覆盖。



我知道C ++只支持类级封装,我想知道我的问题是因为同一个类的成员可以访问对方的私有成员。有没有办法解决这个问题,所以我可以实现我想做什么?谢谢
Taylor

解决方案

gmtime() localtime() ctime() asctime()返回指向静态数据的指针。因此,后续调用可能会覆盖先前调用写入的信息。这也意味着这些调用不是线程安全的,虽然在这种情况下不涉及多个线程。



其他答案提供了这种限制的可能的解决方法。


This is a follow up post from Instance-level encapsulation with C++.

I've defined a class and created two objects from that class.

#include <iostream>
#include <ctime>
#include <string>

using namespace std;

class timeclass {
  private:
  string date;

  time_t gmrawtime, rawtime;
  struct tm * timeinfo;
  char file_date[9];

  void tm_init(int);

public:
  timeclass(int);
  void print_date();
};

void timeclass::tm_init(int y) {
  timeinfo = gmtime(&rawtime);
  timeinfo->tm_year = y - 1900; // timeinfo->tm_year holds number of years since 1900
  timeinfo->tm_mon = 0;
  timeinfo->tm_mday = 1;
  timeinfo->tm_hour = 0;
  timeinfo->tm_min= 0;
  timeinfo->tm_sec= 0;
}

timeclass::timeclass(int y) {
  timeclass::tm_init(y);
  gmrawtime = mktime(timeinfo) - timezone; 
}

void timeclass::print_date() {
  strftime(file_date,9,"%Y%m%d",timeinfo);

  date = string(file_date);
  cout<<date<<endl;
}

/* -----------------------------------------------------------------------*/

int main()
{
  timeclass time1(1991); 
  timeclass time2(1992); 

  time1.print_date(); // Prints 19920101, despite being initialized with 1991
  time2.print_date(); // Prints 19920101, as expected

  return 0;
}

This example is part of a date counter sliced and diced from my main program, but it illustrates my point. I want to have a date counter running for each instance of the class (time1 and time2), but it looks like once I construct the time2 object, the 'timeinfo' variable that I thought was encapsulated in time1 gets overwritten by the time2 constructor.

I am aware that C++ supports only class-level encapsulation, and am wondering if my problem is because members of the same class have access to one another's private members. Is there a way around this, so I can achieve what I want to do? Thank you, Taylor

解决方案

gmtime(), localtime(), ctime() and asctime() return a pointer to static data. So subsequent calls may overwrite information written by previous calls. This also means that these calls are not thread-safe although in this case multiple threads are not involved.

Other answers have provided possible workarounds for this limitation.

这篇关于私人类成员未完全封装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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