Question about constructors

This is a discussion on Question about constructors within the C++ Programming forums, part of the General Programming Boards category; What does the following code do? Code: class Foo {}; int main() { Foo(); } Is a Foo object created ...

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

    Question about constructors

    What does the following code do?

    Code:
    class Foo
    {};
    
    int main()
    {
    	Foo();
    }
    Is a Foo object created and then immediately destroyed? I've seen this used in a few examples and I don't understand what the purpose of Foo() is here.

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    9
    When you declare a function Foo(); inside the class Foo, it will be a constructor...
    in that case it does nothing... but for instance, if you had a class like this

    Code:
    Class Foo{
    public:
    int x, int y;
    Foo(int a, int b){ x = a; y = b; }
    };
    when you declared an object with a and b it would call the constructor...
    Code:
    Foo FooObj(1, 2);
    and then FooObj.x would be 1 and FooObj.y would be 2... something like this :P...
    Last edited by Hirosh; 09-18-2009 at 07:10 AM.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I was more referring to the fact that main calls a constructor without binding the created object to a variable.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Can you give us a real example, with Foo defined?

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    What's wrong with the example above? It compiles without warnings, and I have no idea what it does.

    In particular, if Foo is a class, what does this do when used outside the class body:

    Code:
    Foo();

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Memloop View Post
    What's wrong with the example above? It compiles without warnings, and I have no idea what it does.

    In particular, if Foo is a class, what does this do when used outside the class body:

    Code:
    Foo();
    Calls the constructor, creates a temporary object, then discards the temporary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  2. A question about class members and constructors
    By Megidolaon in forum C++ Programming
    Replies: 5
    Last Post: 01-30-2009, 03:01 PM
  3. question about constructors and exceptions
    By Elkvis in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2008, 06:15 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Replies: 2
    Last Post: 12-17-2001, 06:40 PM

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