C++ problem

Sharky Forums


Results 1 to 5 of 5

Thread: C++ problem

  1. #1
    Expensive Sushi
    Join Date
    Aug 2001
    Location
    Minnesota
    Posts
    7

    C++ problem

    Is there a way to initialize a variable in a header file, without making it const static, because I will need to increment it later.

    The problem I am running into is that for this project, I cannot edit the main driver file, only the header and definition files. And I need a variable that initially gets initialized, but not every time after that when the default constructor is called.
    Does that make sense? I can explain it in more detail if neccessary.

  2. #2
    Reef Shark
    Join Date
    Oct 2000
    Posts
    397

    Re: C++ problem

    Originally posted by nmayotte
    Is there a way to initialize a variable in a header file, without making it const static, because I will need to increment it later.

    The problem I am running into is that for this project, I cannot edit the main driver file, only the header and definition files. And I need a variable that initially gets initialized, but not every time after that when the default constructor is called.
    Does that make sense? I can explain it in more detail if neccessary.
    You can declare a variable 'static' without the 'const' prefix. However, declaring a 'static' variable in the header does not actually create one. You then need to define the actual variable in a source file.

  3. #3
    Sushi
    Join Date
    Dec 2002
    Location
    Jassy City
    Posts
    1
    hey man.. did u try to declare the variable with the keyword "extern" in front of const int......... would look for e.g. like:
    extern int myvar;

    declare it like this in the library headaer and will be accessable anywhere else.. please let me know if it worked...

  4. #4
    Expensive Sushi TPoise's Avatar
    Join Date
    Dec 2002
    Location
    Tuscaloosa, AL
    Posts
    6
    Well if you can't edit the header files from your project, then most likely you can do it without editing it. But if you MUST go about your way of doing things... Use the extern feature in C++, it lets you create a "global" variable across multiple files that you can edit later. Google extern for more reference or your C++ book

  5. #5
    Registered User peterlak's Avatar
    Join Date
    Dec 2002
    Location
    Bolton, Ontario, canada
    Posts
    8
    no

Posting Permissions

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