CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2010
    Posts
    1

    Constructor question

    I was writing a brief program testing scope with a class and discovered a line that I do not understand. I fixed the problem but my question remains.

    Here is my class.
    Code:
    class aClass
    {
    public:
      aClass()
      { 
    	  std::cout << "i'm alive\n";
      }
      ~aClass()
      { 
    	  std::cout << "i'm dead\n";
      }
    };
    Here is the fixed code. It ouputs "i'm alive" and "i'm dead" then finishes.
    Code:
    int main(int argc, char * argv[])
    {
      aClass A;
    }
    Here is my questionable code.
    Code:
    int main(int argc, char * argv[])
    {
      aClass A();
    }
    I thought that by using A() I would be calling it's constructor. The code compiles but outputs nothing. I tried overloading the () operator, with a similar short message but nothing was outputted. What is the code aClass A() doing? Newbie question, thanks.
    Last edited by Andreas Masur; January 31st, 2010 at 01:05 PM. Reason: Fixed code tags...

  2. #2
    Join Date
    Jun 2008
    Posts
    592

    Re: Constructor question

    Take this code as an example

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Obj
    {
    };
    
    int main()
    {
        Obj Test();
        Test();
        return 0;
    }
    
    Obj Test()
    {
        cout << "Ran" << endl;
        return Obj();
    }
    What is highlighted will tell you what you did . You did a forward declare on a function you didn't define
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  3. #3
    Join Date
    Dec 2009
    Posts
    145

    Re: Constructor question

    Quote Originally Posted by Joeman View Post
    Take this code as an example

    Code:
    #include <iostream>
    
    using namespace std;
    
    class Obj
    {
    };
    
    int main()
    {
        Obj Test();
        Test();
        return 0;
    }
    
    Obj Test()
    {
        cout << "Ran" << endl;
        return Obj();
    }
    What is highlighted will tell you what you did . You did a forward declare on a function you didn't define
    Your code doesn't at all imply any educational purpose, only for those who want to expose their 'obfuscatabity' of code at spare time. Calling ctor 2 times exhibits a combination of low and fat code leading to bloats.

  4. #4
    Join Date
    Jun 2008
    Posts
    592

    Re: Constructor question

    The code wasn't to show how to keep code optimized. You're pointing out code I placed there as something to fill the return with. I could have easily wrote non compiling code or undefined code, but I thought it would be better as a working example. The return had nothing to do with the answer or the question.

    Quote Originally Posted by spencer.d
    I thought that by using A() I would be calling it's constructor.
    I showed him why this wasn't so. It was an example and not for production purposes

    Also

    Quote Originally Posted by Ledidas
    Calling ctor 2 times exhibits a combination of low and fat code leading to bloats.
    Why do you think 2 ctors are getting called for?

    Code:
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    class Object
    {
        public:
    
        Object()
        {
            cout << "constructed" << endl;
        }
    
        Object( const Object& Obj )
        {
            cout << "copy constructed" << endl;
        }
    
        ~Object()
        {
            cout << "destructed" << endl;
        }
    
        Object& operator = ( const Object& Obj )
        {
            cout << "assignment" << endl;
            return *this;
        }
    };
    
    Object Construct()
    {
        return Object();
    }
    
    int main()
    {
        Object Test = Construct();
        return 0;
    }
    This outputs one constructor and one destructor. not 2
    Last edited by Joeman; February 1st, 2010 at 07:02 AM.
    0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
    0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000
    0000 0000 0000 0000

  5. #5
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Constructor question

    Quote Originally Posted by Ledidas View Post
    Your code doesn't at all imply any educational purpose, only for those who want to expose their 'obfuscatabity' of code at spare time. Calling ctor 2 times exhibits a combination of low and fat code leading to bloats.
    What exactly is it, you trying to state?
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor question

    Quote Originally Posted by Ledidas View Post
    Your code doesn't at all imply any educational purpose,
    Sure it does. It shows where that line of code will actually mean something, and that is to forward declare a function.

    As a matter of fact, it teaches a lot -- that functions can be declared (and the proof is in the example), and that creating no parameter, non-dynamic objects is not done using the () syntax.
    Calling ctor 2 times exhibits a combination of low and fat code leading to bloats.
    So were you fooled by this?
    Code:
    Object Construct()
    {
        return Object();
    }
    //...
    Object Test = Construct();
    A great trick interview question -- how many times is the constructor for Object called? If you answer this by saying a number, you're already wrong.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Constructor question

    On related note, does anybody know of a good reason, why declaring a function within a function block is legal? I mean, if functions could only be declared outside of function blocks, this typical beginner's mistake would not be possible and probably the C++ parsers would be a little easier to write. So there must be some counterargument.
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor question

    Quote Originally Posted by treuss View Post
    On related note, does anybody know of a good reason, why declaring a function within a function block is legal? I mean, if functions could only be declared outside of function blocks, this typical beginner's mistake would not be possible and probably the C++ parsers would be a little easier to write. So there must be some counterargument.
    It's a holdover from 'C'.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Constructor question

    Quote Originally Posted by Paul McKenzie View Post
    Quote Originally Posted by Treuss
    On related note, does anybody know of a good reason, why declaring a function within a function block is legal? I mean, if functions could only be declared outside of function blocks, this typical beginner's mistake would not be possible and probably the C++ parsers would be a little easier to write. So there must be some counterargument.
    It's a holdover from 'C'.

    Regards,

    Paul McKenzie
    I read somewhere that C coders used local function declarations as a primitive form of encapsulation; like, say, you have a complex data structure, a set of functions operating on its "internals" and a set of "public" functions; the latter declares locally the former passing them around between each other as function pointers... I know, this is programming paleontology ...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured