0

I am new to c++ programming. I am trying few things I tried using directive in following way as shown in below program just for trail it is throwing error

IDE used:VS 2015

language :VC++

Project type: library
Error occured is Error: Expected an identifier

This is in Stdafx.h

#define MANAGED_PUBLIC  public ref

This is in trail.h

using namespace System;

namespace trail {

    MANAGED_PUBLIC class Class1
    {
        // TODO: Add your methods for this class here.
    };
}

I wanted to use MANAGED_PUBLIC instead of using public ref each and every time in whole project

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Why would you want to do this? `public ref` is well known, MANAGED_PUBLC would rather confuce others. I do not recommend to do so. – Aconcagua Jun 03 '16 at 09:14
  • i am modifying some legacy code , They used #define MANAGED_PUBLIC __gc public and new syntax refers ' ref ' instead ' '__gc' – saranhya narakedamilli Jun 03 '16 at 09:19
  • Off-topic but I must say I find `public ref class` a lot easier to read than `MANAGED_PUBLIC class`. `ref class` screams CLI (which is of critical importance in the design of the library); `MANAGED_PUBLIC` just screams. – Tom Blodget Jun 04 '16 at 20:13

2 Answers2

1

You cannot do that. public ref is a context sensitive keyword. You cannot bury it down under a macro. C++/CLI compiler would process it differently than a regular compiler hence the macro outcome won't be public ref. You must type it everywhere.

You can use two macros:

#define MANAGED_PUB public
#define MANAGED_REF ref

MANAGED_PUB MANAGED_REF  class Class1
{
   // TODO: Add your methods for this class here.
};
Ajay
  • 18,086
  • 12
  • 59
  • 105
  • Shouldn't the macros be replaced by the preprocessor before compilation? Or has MS changed this entirely for C++/CLI? – Aconcagua Jun 03 '16 at 09:44
  • I am still finding good source for the answer. But I am sure `#define MANAGED_CLASS ref class` type of thing won't work. – Ajay Jun 03 '16 at 09:45
  • But the legacy code what i have was using similar syntax with "#define MANAGED_PUBLIC __gc public " htat was supported in .net 2.0 – saranhya narakedamilli Jun 03 '16 at 10:00
  • You may try with old-syntax of Managed C++, but newer version won't allow it. – Ajay Jun 03 '16 at 10:01
  • I tried finding actual reason of *compiler - pre-processor - compiler* order where macro is not expanded by CLI compiler, hence the error. But failed to locate the source/article/SO question. – Ajay Jun 03 '16 at 10:03
  • So what could be resolution manually i has to manage every where with public ref instead of using directive? – saranhya narakedamilli Jun 03 '16 at 10:40
  • See my updated answer which states you need to build two macros. – Ajay Jun 03 '16 at 10:42
  • i tried same thing it was throwing error saying " Erorr:Expected an identifier" – saranhya narakedamilli Jun 03 '16 at 10:50
  • Yes, you are right. It still wants `public ref` as single keyword. Replacing it is only good option IMO/ – Ajay Jun 03 '16 at 10:55
  • Actually, [“ref class” is a single token in the lexer](https://blogs.msdn.microsoft.com/hsutter/2003/11/23/ccli-keywords-under-the-hood/). – Tom Blodget Jun 04 '16 at 20:17
0

You might try a compiler flag: -DMANAGED_PUBLIC="public ref" for your legacy code (quotes are stripped according to msdn).

Aconcagua
  • 24,880
  • 4
  • 34
  • 59