我是C ++编程的新手。我使用cpp文件来保存各种变量,我有点担心我在这里做错了什么。我有一个只包含一些变量的.cpp文件。
#include "Variables.h"
using namespace::std;
char ListItem[260] = "<Choose Location>";
string sqlDirectiveMessage = "";
int locationIndex = -1;
int selectionIndex = 0;
int dataGatheredFromIndex = 0;
SQLCHAR retconstring[1024];
和Variables.h
#pragma once
#include <Windows.h>
#include <sqlext.h>
#include <sql.h>
#include <vector>
#include <sqltypes.h>
#include <string>
using namespace::std;
extern char ListItem[];
extern string sqlDirectiveMessage;
extern int locationIndex;
extern int selectionIndex;
extern int dataGatheredFromIndex;
extern SQLCHAR retconstring[1024];
一段相关代码:
case IDC_ADD:
{
int test = 0;
HWND listbox = GetDlgItem(hwnd, IDC_LIST3);
selectionIndex = (int)SendMessage(listbox, LB_GETCURSEL, 0, 0);
dataGatheredFromIndex = (int)SendMessage(listbox, LB_GETITEMDATA, selectionIndex, 0);
}
break;
现在在这种情况下,如果我在VS 2017中对一些变量进行观察,则selectionIndex和locationIndex似乎工作正常,测试未定义,dataGatheredFromIndex也是如此。我做错了吗?
答案 0 :(得分:2)
编译器可以优化代码,以便省略或以不同方式排序操作。优化通常在发布构建配置中启用。
代码中的变量test
是此类优化的受害者,因为没有代码可以读取它。如果在启用优化时代码无法访问变量值,则应忽略变量值。