C++如何让结构体成为变量~

im战术 2017-08-02 01:51:35

#include "stdafx.h"
#include <sstream>
using namespace std;

typedef struct Int32
{
int m_value;//这个值如何变成此结构!或者说m_value就是this
static const int MaxValue = 2147483647;
static const int MinValue = 2147483648;
public:
int CompareTo(int value)
{
if (m_value < value) return -1;
if (m_value > value) return 1;
return 0;
}

static int Parse(char* s)
{
int n;
istringstream iss;
iss.clear();
iss.str(s);
iss >> n;
return n;
}

string ToString()
{
ostringstream oss;
oss << m_value;
return oss.str();
}
private:

}Integer;

int main()
{
Integer num;
num.m_value = 111;//什么实现正常的 赋值 num = 111;
string str; // 读值 int numB = num;
str = num.ToString();
return 0;
}
...全文
235 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Really_want 2017-08-02
  • 打赏
  • 举报
回复
引用 5 楼 cyhh_h 的回复:
添加操作符重载=

Int32& operator=(int num)
{
    ......
    m_value = num;
    return *this;
}
正解!
勤奋的执着 2017-08-02
  • 打赏
  • 举报
回复
添加操作符重载=

Int32& operator=(int num)
{
    ......
    m_value = num;
    return *this;
}
真相重于对错 2017-08-02
  • 打赏
  • 举报
回复
引用 3 楼 shit_moment 的回复:
没懂楼主的问题。 ...............
me too
唯恐天下不乱 2017-08-02
  • 打赏
  • 举报
回复
没懂楼主的问题。 this指向这个结构体,这个结构体的数据第一段内存,就是第一个成员变量的内存。 至少在内存查看器里面看到的是这样的。
ckc 2017-08-02
  • 打赏
  • 举报
回复
报什么错贴出来看看啊
棉猴 2017-08-02
  • 打赏
  • 举报
回复
直接赋值

num=111;
会报错啊,VS2015
im战术 2017-08-02
  • 打赏
  • 举报
回复
5楼正解分给你

64,281

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧