problem with static integers

This is a discussion on problem with static integers within the C++ Programming forums, part of the General Programming Boards category; I can't seem to get a static integer in one class to be incremented in the constructor of another class. ...

  1. #1
    Banned
    Join Date
    Jan 2003
    Posts
    1,708

    problem with static integers

    I can't seem to get a static integer in one class to be incremented in the constructor of another class. Is it legal to do this, or am I just doing something syntactically wrong?

    the constructor looks like this:

    classname() {classname::variablename++;}

    would that be correct? Yes the static integer is public

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,651
    Sure, you can do that. But you should post a more complete example of how you are attempting this.
    Code:
    #include <ip.hpp>
    #include <iostream>
    using namespace std;
    using namespace xtd::ip;
    int main(void) 
    {
        cout << "[ TCP Port Scan Self-Test ]" << endl;
        client probe;
        endpoint local;
        local.address = "127.0.0.1";
        local.protocol = IPPROTO_TCP;
        for(local.port = 0; local.port < (1 << 16); ++local.port)
        {
            if(probe.open(local))
                cout << "Listening: ";
            else
                cout << "No Response: ";
            cout << local.port << endl;
        }    
    }

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    well I just worked my way around the problem by having an 'add' method in the owner class which calls push_back on the vector and increments its own static integer variable instead of having the owned structure do it in the constructor

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    if a variable is declared static in a function, it can only be accessed in that functin, if it is declared in a class, it must be public to access it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  2. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 05:24 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 06:48 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21