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

C++语言 码拜 8年前 (2016-01-29) 809次浏览
本人有遇到一个问题,把问题单独抽离出来,代码如下,其中,给派生类增加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 个 ==========
解决方案:40分
volatile是针对数据的,例如变量,但是类中有方法,所以对类的使用可能报错

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明关于对派生类使用volatile关键字
喜欢 (0)
[1034331897@qq.com]
分享 (0)