CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2010
    Posts
    18

    #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:EF03"

    Should I be declaring the wrapper classes differently? The purpose here is to expose native code to the client.

    Thanks in advanced.
    Last edited by TFobear; August 3rd, 2010 at 09:17 AM.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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)
          {}
       };
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Jul 2010
    Posts
    18

    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 ...
    Last edited by TFobear; August 3rd, 2010 at 09:06 AM.

  4. #4
    Join Date
    Jul 2002
    Posts
    2,543

    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;

  5. #5
    Join Date
    Jul 2010
    Posts
    18

    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

  6. #6
    Join Date
    Jul 2002
    Posts
    2,543

    Re: #define to static const int

    Use "literal" keyword:

    literal int DEF1 = 0x1010;

    This is the same as

    const int DEF1 = 0x1010;

    in C#.

  7. #7
    Join Date
    Jul 2010
    Posts
    18

    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
    Last edited by TFobear; August 3rd, 2010 at 02:31 PM. Reason: link is an eBook, not an article

  8. #8
    Join Date
    Aug 2010
    Posts
    51

    Smile 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured