object() and object;

This is a discussion on object() and object; within the C++ Programming forums, part of the General Programming Boards category; Hello I've been having headaches for 2 days because I thought someobject o; is the same as someobject o(); Now ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    object() and object;

    Hello

    I've been having headaches for 2 days because I thought someobject o; is the same as someobject o();

    Now I wonder what is the difference betwen those two?

    My program was crashing because I had someobject o; which prevented the expected constructor behavior.

    When I changed it to someobject o(); everything worked as it should..

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    There shouldn't be a difference. Can you show some code?

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Sure..

    Inside main:

    client_session o; //will make it crash

    client_session o(); //this wont make it crash

    Code:
    class client_session {
    public:
    	client_session() :
    		_io_service(),
    		_client(_io_service, *this) { }
    	
    	boost::asio::io_service _io_service;
    	client _client;
    };
    
    class client : public connection {
    public:
    	client(boost::asio::io_service& io_service, client_session &session) :
    		connection(io_service),
    		_session(session) { }
    
    	client_session &_session;
    };
    
    class connection {
    public:
    	connection(boost::asio::io_service &io_service) : _socket(io_service) { }
    	boost::asio::ip::tcp::socket _socket;
    };

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by l2u View Post
    I've been having headaches for 2 days because I thought someobject o; is the same as someobject o();

    Now I wonder what is the difference betwen those two?
    The first declares an object 'o' of type someobject. The second declares a function 'o' which takes no parameters and returns a someobject.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 08:49 AM
  2. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 05:33 AM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 09:38 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 05:33 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 05:24 PM

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