Conflicting class keyword

Discussion in 'C++' started by kiran kumr, Jan 10, 2007.

  1. kiran kumr

    kiran kumr Guest

    Hi,

    we have the following scenario:

    1. A header file that is used in multiple modules a c application and a C++
    application.
    2. This file has a variable named class[SIZE].
    3. How do I declare class now without conflicting with the other variable
    class in the common header file.

    I cannot rename the variable class and getway without including the header
    file.

    -Regards,
    kiran kumr, Jan 10, 2007
    #1
    1. Advertising

  2. kiran kumr wrote:
    > Hi,
    >
    > we have the following scenario:
    >
    > 1. A header file that is used in multiple modules a c application and a C++
    > application.
    > 2. This file has a variable named class[SIZE].
    > 3. How do I declare class now without conflicting with the other variable
    > class in the common header file.
    >
    > I cannot rename the variable class and getway without including the header
    > file.


    Perhaps

    #undef class
    #define class old_class
    #include "old_header.h"
    #undef class

    Does that count as renaming the variable?

    I think this should work, although AFAIK it is illegal to re#define
    C++ keywords.

    HTH,
    - J.
    Jacek Dziedzic, Jan 11, 2007
    #2
    1. Advertising

  3. kiran kumr

    Jim Langston Guest

    "kiran kumr" <> wrote in message
    news:...
    > Hi,
    >
    > we have the following scenario:
    >
    > 1. A header file that is used in multiple modules a c application and a
    > C++
    > application.
    > 2. This file has a variable named class[SIZE].
    > 3. How do I declare class now without conflicting with the other variable
    > class in the common header file.
    >
    > I cannot rename the variable class and getway without including the
    > header
    > file.
    >
    > -Regards,


    If each compiliation unit is to have it's own copy of the variable then make
    it static.
    static whatever class[SIZE];

    If each compiliation unit is to share the one instance of the variable, then
    declare it external:
    extern whatever class[SIZE];

    and in one, and only one, compilation unit (one of the .cpp files) declare
    it as a normal global variable:
    whatever class[SIZE];
    Jim Langston, Jan 11, 2007
    #3
  4. kiran kumr

    Colander Guest

    Jim Langston wrote:

    > "kiran kumr" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > we have the following scenario:
    > >
    > > 1. A header file that is used in multiple modules a c application and a
    > > C++
    > > application.
    > > 2. This file has a variable named class[SIZE].
    > > 3. How do I declare class now without conflicting with the other variable
    > > class in the common header file.
    > >
    > > I cannot rename the variable class and getway without including the
    > > header
    > > file.
    > >
    > > -Regards,

    >
    > If each compiliation unit is to have it's own copy of the variable then make
    > it static.
    > static whatever class[SIZE];
    >
    > If each compiliation unit is to share the one instance of the variable, then
    > declare it external:
    > extern whatever class[SIZE];
    >
    > and in one, and only one, compilation unit (one of the .cpp files) declare
    > it as a normal global variable:
    > whatever class[SIZE];


    I think you overlooked that in C++ class is an keyword, in C it isn't.

    so Legal c code:
    int class = 42;
    And illegal C++ code:
    int class = 42;

    Yes there are things that C can do that C++ can't...
    Colander, Jan 11, 2007
    #4
  5. kiran kumr

    BobR Guest

    Colander wrote in message ...
    >Jim Langston wrote:
    >> If each compiliation unit is to have it's own copy of the variable then

    make
    >> it static.
    >> static whatever class[SIZE];
    >> If each compiliation unit is to share the one instance of the variable,

    then
    >> declare it external:
    >> extern whatever class[SIZE];
    >> and in one, and only one, compilation unit (one of the .cpp files) declare
    >> it as a normal global variable:
    >> whatever class[SIZE];

    >
    >I think you overlooked that in C++ class is an keyword, in C it isn't.
    >
    >so Legal c code:
    >int class = 42;
    >And illegal C++ code:
    >int class = 42;
    >
    >Yes there are things that C can do that C++ can't...


    So, in C, this is legal?

    int struct = 42;

    --
    Bob R
    POVrookie
    BobR, Jan 11, 2007
    #5
  6. kiran kumr

    Jim Langston Guest

    "BobR" <> wrote in message
    news:0Lzph.691980$...
    >
    > Colander wrote in message ...
    >>Jim Langston wrote:
    >>> If each compiliation unit is to have it's own copy of the variable then

    > make
    >>> it static.
    >>> static whatever class[SIZE];
    >>> If each compiliation unit is to share the one instance of the variable,

    > then
    >>> declare it external:
    >>> extern whatever class[SIZE];
    >>> and in one, and only one, compilation unit (one of the .cpp files)
    >>> declare
    >>> it as a normal global variable:
    >>> whatever class[SIZE];

    >>
    >>I think you overlooked that in C++ class is an keyword, in C it isn't.
    >>
    >>so Legal c code:
    >>int class = 42;
    >>And illegal C++ code:
    >>int class = 42;
    >>
    >>Yes there are things that C can do that C++ can't...

    >
    > So, in C, this is legal?
    >
    > int struct = 42;


    You're absolutely right, I just typed in the OP's variable name on auto
    pilot. Yeah, can't use any keyword as a var name (including struct).

    So, no, int struct = 42; is not legal in c++
    Jim Langston, Jan 12, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?amF2YXRvcGlh?=

    [ASP.NET 2.0] ChangePassword class conflicting with my page

    =?Utf-8?B?amF2YXRvcGlh?=, Apr 15, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,421
    =?Utf-8?B?amF2YXRvcGlh?=
    Apr 17, 2006
  2. Ben Engbers
    Replies:
    0
    Views:
    489
    Ben Engbers
    Feb 7, 2007
  3. Replies:
    6
    Views:
    449
    Peter Otten
    May 10, 2007
  4. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    349
  5. Hongyu
    Replies:
    2
    Views:
    290
    Hongyu
    Aug 22, 2008
Loading...

Share This Page