Loading Different Headers Variable based


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Loading Different Headers Variable based

  1. #1
    Join Date
    Jul 2007
    Posts
    12

    Loading Different Headers Variable based

    if i have two different headers one MyHeaderFr.h and the other MyHeaderUS.h and i have a string var that hold "US" or "FR" how do i load the header files accordingly?

    Thank You,

  2. #2
    Join Date
    Jan 2006
    Posts
    55
    You have string variable with same name in both headers?If yes, the you can enclose them inside a namespace and use accordingly.

  3. #3
    Join Date
    Jul 2007
    Posts
    12
    sorry if my original post was not clear but i want to load only one of the headers. So i load MyHeaderFR.h or MyHeaderUS.h.

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    I really have lost you completely. What exactly are you trying to do and what is the problem you're facing? Can't you simply #include the header that you need?
    Danny Kalev

  5. #5
    Join Date
    Jul 2007
    Posts
    12
    I sorry let me try to explain it better.

    I have for example i have a const string in two different headers. One is US.h the other FR.h
    Code:
    ---US.h---
    const string welcome = "HI";
    ---FR.h---
    const string welcome = "Bonjour";
    i my main code i have some thing like this
    Code:
    string lang = "US";
    depending on the value of lang i want to loader either the US.h or FR.h

  6. #6
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    just use a #define combined with #if like
    Code:
    #include "language_selector_header.hpp"
    
    #if defined USE_FRENCH_HEADER
    #include "FR.h"
    #elif defined USE_USE_HEADER
    #include "US.h"
    #elif
    /// ...
    #endif
    /// '''
    and un the file language_selector_header.hpp define a variable for the language you would like to use

    like
    Code:
    #define USE_MANDARIN_LANGUAGE
    or something more clever.
    Insome compilers you can even pass a preprocessor variable like that on the command line...


    Cheers,

    D
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  7. #7
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Are you looking to implement this at "run time" or at "compile time"?

  8. #8
    Join Date
    Nov 2003
    Posts
    4,118
    You can't use a string object, which is a runtime notion (as are all objects) to make a decision that takes place conceptually before compilation time! When the preprocessor evaluates the #ifdef and #include expressions, the notion of strings and objects is completely inexistent yet. Think of the preprcessor as a clever text editor, not as anything that's even close to a C++ compiler.
    So you need to use another macro, not a string, in order to determine during the preprocessing stage which header should be #included.
    I also have to warn you that embedding string objects in a header file is a bad idea. The problem is that the header can be #included multiple times in each translation unit, and the result is multiple copies of string objects. The linker rarely has the cleverness to collapse all these strings into a single instance. A much better solution is to #define a macro in the header file and use that macro at runtime to assign a value to a shared string. Alternatively, use environment variables to determine the locale at runtime, and the load the appropriate strings from a file.
    Danny Kalev

  9. #9
    Join Date
    Jul 2007
    Posts
    12
    by doing this
    Code:
     #include "language_selector_header.hpp"
    
    #if defined USE_FRENCH_HEADER
    #include "FR.h"
    #elif defined USE_USE_HEADER
    #include "US.h"
    #elif
    /// ...
    #endif
    /// '''
    seem like i have to hard code in what language to use. I was hoping to uses a string/char variable that could be assigned after reading data from a file.

  10. #10
    Join Date
    Nov 2003
    Posts
    4,118
    Then use a string table stored in a file, and load that string table into the program at runtime. You can do it more elegantly with locales, if you have some experience with this library.
    Danny Kalev

Similar Threads

  1. Replies: 3
    Last Post: 07-13-2006, 11:43 PM
  2. Replies: 2
    Last Post: 04-13-2003, 06:05 PM
  3. Loading HTML text into a variable dynamicly
    By Manoj Shah in forum Web
    Replies: 1
    Last Post: 07-02-2001, 11:50 AM
  4. creating a dynamic object based upon a variable???
    By Patrick Buchanan in forum ASP.NET
    Replies: 3
    Last Post: 11-29-2000, 01:06 PM
  5. Variable based array size
    By Nikhil in forum ASP.NET
    Replies: 2
    Last Post: 05-11-2000, 04:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center