Constructor is not called for static object?

This is a discussion on Constructor is not called for static object? within the C++ Programming forums, part of the General Programming Boards category; What is going on here? Code: const int value = 1; // x is a public int Test::Test() : x(value) ...

  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
    21,648
    Post the smallest and simplest compilable program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,799
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    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, 10: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, 12:45 PM

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