Class Public Variables not holding their value

This is a discussion on Class Public Variables not holding their value within the C++ Programming forums, part of the General Programming Boards category; Ok. So I have created my class and all this other jazz, but when I try/catch my class's constructor, it ...

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398

    Class Public Variables not holding their value

    Ok. So I have created my class and all this other jazz, but when I try/catch my class's constructor, it seems my main function is reading some values from variables inside the class and getting unexpected results, at least to me .

    Here is my class+constructor
    Code:
    class server_socket
    {
        private:
            int fsock;
            short int fport;
            struct sockaddr_in faddr;
    
            bool ssocket(void);
            bool sbind(void);
            bool slisten(void);
    
        public:
            int ssocket_error;
            int sbind_error;
            int slisten_error;
    
            server_socket(short int port);
            ~server_socket();
    };
    
    server_socket::server_socket(short int port)
    {
        /* Used for debugging */
        std::cout << "Constructor Called" << std::endl;
    
        /* Initialize variables */
        this->fport = port;
        this->fsock = 0;
        memset(&(this->faddr), 0, sizeof(struct sockaddr_in));
    
        this->ssocket_error = 1;
        this->sbind_error = 2;
        this->slisten_error = 3;
    
        /* Prepare socket */
        if (!ssocket())
            throw ssocket_error;
        if (!sbind())
            throw sbind_error;
        if (!slisten())
            throw slisten_error;
    }
    Here is my main
    Code:
    int main (void)
    {
        server_socket ........ck;
    
        try
        {
            ssck = new server_socket (80);
            std::cout << "Socket Creation Successful!" << std::endl;
            delete ssck;
        }
        catch (int err)
        {
            if (err == ssck->ssocket_error)
                std::cout << "Error in socket()" << std::endl;
            else if (err == ssck->sbind_error)
                std::cout << "Error in bind()" << std::endl;
            else if (err == ssck->slisten_error)
                std::cout << "Error in listen()" << std::endl;
            else
                std::cout << "Unknown Error: (" << err << ")" << std::endl;
    
            /* Taking a look at error variable values */
            std::cout << "socket: " << ssck->ssocket_error << std::endl;
            std::cout << "  bind: " << ssck->sbind_error << std::endl;
            std::cout << "listen: " << ssck->slisten_error << std::endl;
    
            return -1;
        }
    
        return 0;
    }
    The three functions my constructor calls in no way modifies my three error variables.
    So when I fire this bad boy up, I have tested all three cases (where each of the three functions fail), and get results I am not looking for. It is telling me "Unknown Error (2)" or "Unknown Error (3)".

    From this point, I check the values of the member variables and they are the following:
    ssocket_error = 1
    sbind_error = 1
    slisten_error = 0

    So obviously they aren't matching up. I feel I'm dancing around the lines of undefinedness due to this behavior. Anyone see where I am making a problem?
    Last edited by carrotcake1029; 04-12-2009 at 04:10 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,261
    Code:
    /* Taking a look at error variable values */
    std::cout << "socket: " << ssck->ssocket_error << std::endl;
    std::cout << "  bind: " << ssck->sbind_error << std::endl;
    std::cout << "listen: " << ssck->slisten_error << std::endl;
    If the constructor threw, then the object was not created. If the object was not created, you cannot read values from that object.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Aha, I knew there was some undefined behavior. Thank you.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think this has been mentioned before. Throw real exception types (either one of the standard ones or your own ones derived from the standard ones).

    There's also probably very little need to allocate the instance dynamically, so you can put the try block around the creation only. Exceptions allow more centralized error handling, so enclose everything that the instance is going to be used for in a try block - perhaps at the level of a calling function.

    If you really want to keep your error codes, they should probably be static const members (or an enum).

    Code:
    server_socket::server_socket(short int port)
    {
        /* Used for debugging */
        std::cout << "Constructor Called" << std::endl;
    
        /* Initialize variables */
        this->fport = port;
        this->fsock = 0;
        memset(&(this->faddr), 0, sizeof(struct sockaddr_in));
    
        /* Prepare socket */
        if (!ssocket())
            throw std::runtime_error("Error in socket");
        if (!sbind())
            throw std::runtime_error("Error in bind");
        if (!slisten())
            throw std::runtime_error("Error in listen");
    }
    
    int main()
    {
        try {
            server_socket ssck(80);
            //everything you want to do with it
        }
        catch (std::exception& e) {
            std::cout << e.what() << '\n';
        }
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring SDL_Rect variables in a class?
    By pwnz32 in forum C++ Programming
    Replies: 10
    Last Post: 07-27-2008, 08:12 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. C++ to C Conversion
    By dicon in forum C Programming
    Replies: 7
    Last Post: 06-11-2007, 09:38 PM
  4. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 03:54 PM
  5. Functions in class and class variables.
    By Karakus in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2006, 03:29 AM

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