关于对派生类使用volatile关键字

Never_Satisfied 2015-05-10 10:36:17
我有遇到一个问题,把问题单独抽离出来,代码如下,其中,给派生类增加volatile关键字后,出错:
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include "testPLUS.h"
int _tmain(int argc, _TCHAR* argv[])
{
volatile testPLUS * x; // 我需要一个 volatile 变量,可是声明为volatile后就报错了,不声明为volatile就没问题。
x = new testPLUS();
x->testBASE_a(); // 编译提示该行出错。
return 0;
}

//testBASE.h : 基类testBASE的 声明。
#pragma once
class testBASE
{
public:
testBASE();
virtual ~testBASE();

void testBASE_a();
};

//testBASE.h : 基类testBASE的 定义。
#include "stdafx.h"
#include "testBASE.h"
testBASE::testBASE()
{}

testBASE::~testBASE()
{}

void testBASE::testBASE_a()
{
return;
}

//testPLUS.h : 派生类testPLUS的 声明。
#pragma once
#include "testBASE.h"
class testPLUS :
public testBASE
{
public:
testPLUS();
~testPLUS();
};

//testPLUS.h : 派生类testPLUS的 定义。
#include "stdafx.h"
#include "testPLUS.h"
testPLUS::testPLUS()
{}

testPLUS::~testPLUS()
{}


编译的输出内容如下:
1>------ 已启动全部重新生成: 项目: ConsoleApplication1, 配置: Debug Win32 ------
1> stdafx.cpp
1> testPLUS.cpp
1> testBASE.cpp
1> ConsoleApplication1.cpp
1>c:\users\zzs\desktop\consoleapplication1\consoleapplication1\consoleapplication1.cpp(10): error C2662: “void testBASE::testBASE_a(void)”: 不能将“this”指针从“volatile testPLUS”转换为“testBASE &”
1> 转换丢失限定符
1> 正在生成代码...
========== 全部重新生成: 成功 0 个,失败 1 个,跳过 0 个 ==========
...全文
128 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yaojifenzzs0 2016-01-29
  • 打赏
  • 举报
回复
volatile是针对数据的,比如变量,但是类中有方法,所以对类的使用可能报错

64,662

社区成员

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

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