#define to static const int
I have my ideas of scope messed up.
I have a native DLL which I am compiling with /CLR in hopes to export Native global functions and constants. To do so I have created a class I am hoping I can call from C# or VB.Net clients statically.. so:
Code:
namespace CompanyNameManaged {
public ref struct CompanyName sealed abstract
{
static void func1(String^ filename, int page); //called from c# like CompanyName.func1("literal", 9);
// These are #defined in a seperate, unmanaged file
ref struct CONSTANTS sealed abstract
{
//From Native Header: #define DEF01 0x0001
const int DEF01 = 0x0001; //doesnt compile, needs to be "Static"
static const int DEF02 = 0x0002; //works, called from DLL as CompanyName.CONSTANTS.DEF02;
static const int DEF03 = 0x0003;
//DEF03 is defined in windows api file "WinGdi.h" I dont believe I am including this file anywhere
//but I believe this is the source of my problem since when I change name (MY_DEF03) it compiles.
}; //ref stuct CONSTANTS
}; //ref struct CompanyName
}; //namespace
SO, when I try to compile with the const "DEF03" I have issues, the error is:
error C2059: syntax error : 'constant'
error C2238: unexpected token(s) preceding ';'
It shouldnt have an issue because I think the scope is "CompanyName::CONSTANTS::DEF03"
Should I be declaring the wrapper classes differently? The purpose here is to expose native code to the client.
Thanks in advanced.
Re: #define to static const int
You just want a class with a bunch of constants? You could make it static instead of sealed abstract.
Code:
static ref struct CONSTANTS
{
const int DEF01;
const int DEF02;
const int DEF03;
CONSTANTS():DEF01(0x0001), DEF02(0x0002), DEF03(0x0003)
{}
};
Re: #define to static const int
I like this solution, in my honest oppinion sealed and abstract are just syntatic sugar I dont really need. Any ways, this doesnt solve my problem though, as the compiler is still seeing them as redefinitions of #define constants.
As this is a native dll that uses these #defines in numerous places, I would like to leave the existing code untouched. However it would be nice to be able to export these definitions to a managed client.
I think what my solution could be... though it seems like a hack to me... is this:
Code:
//NativeDecl.h
void func1(void);
void func2(void);
#ifndef MANAGED_DECL
#define CONST1 0x0001
#define CONST2 0x0002
#endif
//ManagedDecl.h
#define MANAGED_DECL
#include "NativeDecl.h"
static ref struct Foo { const int CONST1, CONST2 };
*edit* That is not a valid solution, the consts generating the errors ("PHYSICALWIDTH" and "PHYSICALHEIGHT") are also defined in WinGdi.h ...
Re: #define to static const int
Macro problem cannot be solved by namespaces. Just undef this macro:
#undef DEF03
...
ref struct CONSTANTS sealed abstract
...
static const int DEF03 = 0x0003;
Re: #define to static const int
thanks Alex, I have done this and am moving on.
However, the suggestion to make the class static rather than sealed abstract - produces error c2597 "illegal reference to non-static member"
example:
Code:
public ref struct Toolkit sealed abstract
{
static ref struct Module1
{
static ref struct Constants
{
const int DEF1;
Constants() : DEF1(0x1010) { }
};
};
static ref struct Module2
{
void func1(IntPtr x);
};
};
void Toolkit::Module2::func1(IntPtr x)
{
OtherFunc( x, Module1::Constants::DEF1 ); //error c2597
}
MSDN's example shows totally different situation. http://msdn.microsoft.com/en-us/libr...a2(VS.71).aspx
Re: #define to static const int
Use "literal" keyword:
literal int DEF1 = 0x1010;
This is the same as
const int DEF1 = 0x1010;
in C#.
Re: #define to static const int
Thanks yet again!!
... thats funky, i guess they needed a way to differentiate native const types and managed ones.
I am, as you can tell, a C++/Cli newbie. I have found a great *eBook* though that delves into specifics, including questions I have been having about DllMain and inititialization issues.
http://int6.net/ebook/Expert.Cpp.Cli.NET.pdf
Re: #define to static const int
hi,
i guess they needed a way to differentiate native const types and managed ones.
regards,
phe9oxis,
http://www.guidebuddha.com