Thread: Constructor is not called for static object?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    Constructor is not called for static object?

    What is going on here?

    Code:
    const int value = 1;
    
    // x is a public int
    Test::Test() : x(value)
    {}
    Code:
    Test& Test::instance()
    {
    	static Test instance;
    	return instance;
    }
    Code:
    Test test = Test::instance();
    cout << test.x << endl; // This produces 0 and not 1!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Code:
    #include <iostream>
    
    const int x = 1;
    
    class Test
    {
    	public:
    		const int x;
    		static Test& instance()
    		{
    			static Test instance;
    			return instance;
    		}
    	private:
    		Test();
    		Test(const Test&);
    		Test& operator=(const Test&);
    		
    };
    
    // x is a public int
    Test::Test() : x(x)
    {}
    
    int main()
    {
    	Test& test = Test::instance();
    	
    	std::cout << test.x << std::endl;
    }
    I figured out what the problem is: I thought the initialization list would use the global variable x when initializing the class variable x, but it doesn't.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Try
    Code:
    // x is a public int
    Test::Test() : x(::x)
    {}
    ...and it should output 1.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So to answer your question: yes, it does call the constructor. But only once.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Telling a shared_ptr not to delete object?
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 08-16-2008, 04:26 AM
  2. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Help for the c error
    By robert_sun in forum C Programming
    Replies: 4
    Last Post: 05-13-2004, 01:38 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM