DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Default or Empty constructor question

  1. #1
    Join Date
    Oct 2007
    Posts
    369

    Default or Empty constructor question

    I guess I'm getting fuzzy on something pretty basic. I need more caffeine this morning.
    Correct anything I say wrong.

    For primitive type, say int,
    int i;
    allocates the memory, but does not "call a constructor"; i.e., the variable is not initialized. But
    int i();
    allocates the memory and calls the default, empty constructor which sets i to 0.

    Similarly, for class M:
    M m;
    allocates memory, but does not call the empty constructor (default or defined), but
    M m();
    *does* call the (default or defined) empty constructor.

    Right?

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    I am almost certain that

    M m;

    calls the CTOR right away.

    I am also pretty sure that atomic types are not objects, the ctor you invoke for int is just a special command to the compiler that *look* like a ctor but is not. I don't think even java went so far as to remap standard types into full objects.

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    Curious, I did an
    int i(3);

    and looked at the assembly.
    It says:
    mov dword ptr _igarbled, 3

    which means no function was called, its exactly the same code as

    int i = 3;
    and
    int i;
    i = 3;

    if that is worth anything. ALso, debugging and fighting with it, under no circumstances could I force the machine to step into or acknowledge the line
    int i(3);
    as a function of any type.

  4. #4
    Join Date
    Oct 2007
    Posts
    369
    You are certainly correct that primitive types are really objects. I was speaking notionally. And I was certainly wrong that
    M m;
    doesn't invoke the default constructor for M. It does.

    What I was struggling with is class members in M that are referred to in the constructor of M. For instance
    Code:
    class M
    {
    public:
        M() : field2() {}
    private:
       T field1;
       T field2;
    };
    For field1, the default constructor is invoked. For field2, no constructor is called. This is only if the default (or empty) constructor is *not* defined. If it is explicitly defined, you'll get an error because you didn't specify which constructor to call for field1.

    I'm having trouble wrapping my mind around the various rules.

  5. #5
    Join Date
    Oct 2007
    Posts
    369
    I'm approaching clarity.
    Although primitive types are not full objects, you can say this:
    Code:
        int i1; // implicitly defined constructor *not* called (no initialization)
        int i2(); // implicity defined constructor is called (init to 0)
    This generalizes to classes and structs
    Code:
    struct test_t
    {
        int field1[2];
        int field2;
    };
    
        test_t test1; // implicitly defined constructor *not* called (uninitialized)
        test_t test2(); // implicitly defined constructor is called (init each member)
    When used in a struct or class...
    Code:
    class My
    {
    public:
        My() : t2() { // t1 allocated, but not initialized
                          // t2 initialized
        }
    
    private:
        test_t t1;
        test_t t2;
    };
    If the empty constructor is explicitly defined, it *will* be called either way.
    Code:
        My m1; // explicitly defined constructor *is* called
        My m2(); // Is there something wrong with this?????????

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    guess I am unsure what you really are asking.

    In this case, the CTOR Is called:

    class inner
    {
    inner(){;}
    };

    class outer
    {
    inner x;
    }

    outer a_variable; ///BAM! inner ctor is called!

    Is that your question?

    Or, for the question you did ask:

    My m1; // explicitly defined constructor *is* called
    My m2(); // Is there something wrong with this?????????

    nothing is wrong, its the exact same thing written two different ways.

    Now, if what you are wanting is to define the variable and invoke the ctor later, you have to use a pointer as far as I know:

    My *meh = 0; //CTOR is not called!

    code and stuff.

    meh = new My; //NOW it is called.

    Or, if you are asking if you can create a thing to use, just a memory location for storage just like an int and without calling a CTOR, but for a user defined entity? Not easily or cleanly, all I can think of is direct memory manipulation or pointers into a char array with some casting to change the type, such as:

    struct whateva
    {
    int a;
    double d;
    }

    becomes
    char badidea[20];
    *(int*)(&badidea[0]) = 1234;
    *(double*)(&badidea[10]) = 12.34;

    or something? um. No? You avoid the ctor and just manipulate memory, but can you make a case for the ugliness, even with macros, and the potential for bugs and a total mess that this would create?

  7. #7
    Join Date
    Oct 2007
    Posts
    369
    I appreciate the response. Unfortunately, I talk to myself alot, even on groups like this.

    In my last post I was trying to sum up what I thought were true statements about explicit versus implicit default constructors. If anyone sees anything wrong with them, I would appreciate correction.

    When You get to "My m2(); // is anything wrong?" it was because my g++ seemed to think this was a function declaration.
    The following is the full code, and the output
    Code:
    #include <iostream>
    using namespace std;
    
    struct test_t
    {
        int field1[2];
        int field2;
    };
    
    class My
    {
    public:
        My() : t2() {
            cout << "t1.field1[0]=" << t1.field1[0] << " field1[1]=" << t1.field1[1] << " field2=" << t1.field2 << endl;
            cout << "t2.field1[0]=" << t2.field1[0] << " field1[1]=" << t2.field1[1] << " field2=" << t2.field2 << endl;
        }
    
    private:
        test_t t1;
        test_t t2;
    };
    
    int main()
    {
        int i1; // implicitly defined constructor *not* called
        int i2(); // implicity defined constructor is called (init to 0)
    
        test_t test1; // implicitly defined constructor *not* called (uninitialized)
        test_t test2(); // implicitly defined constructor is called (init each member)
    
        My m1; // explicitly defined constructor is called.
        My m2(); // confusion.
    
        return 0;
    }
    Output:

    t1.field1[0]=1629421832 field1[1]=0 field2=2280856
    t2.field1[0]=0 field1[1]=0 field2=0

  8. #8
    Join Date
    Dec 2003
    Posts
    3,366
    For the ints: its initialized to zero, but can you point to a function call or what? I.E. what really, really happens here, is it just a substitution for int i = 0; (I.E. it moves the value 0 into a memory location) or is there and actual, real thing happening here (an actual function call or, worse, a CTOR)?!

    int i1; // implicitly defined constructor *not* called
    int i2(); // implicity defined constructor is called (init to 0)

    -------------------------------------------

    You mean the default, empty CTOR ?
    As far as I know the result of these should be identical: the objects should have random/garbage values for all data members. There should be no difference at all!

    test_t test1; // implicitly defined constructor *not* called (uninitialized)
    test_t test2(); // implicitly defined constructor is called (init each member)

    --------------------------------------------
    I say G++ is wrong then, the parser failed to handle this case properly. Both should call the ctor and they should be identical.

    My m1; // explicitly defined constructor is called.
    My m2(); // confusion.

    I could be wrong, though. You probably know by now that I have a limited grasp of the ultra deep details from the language spec, but if int has an actual ctor, its news to me. And if type A compared to type A() are supposed to give different results, that is also news to me.

  9. #9
    Join Date
    Dec 2003
    Posts
    3,366
    Ok, nothing likes the case you have that confused G++. I cannot find an official looking explain, but several sites simply say "dont do that" and give similar code as an example of "wrong". There must be a reason this is banned, but its apparently supposed to be illegal. I had never tried it before, or do not remember doing so anyway.

  10. #10
    Join Date
    Oct 2007
    Posts
    369
    OK. I was missing it big time.

    Code:
    int main()
    {
        int i1; // implicitly defined constructor *not* called
        int i2(); // This is a declaration of a function that returns an int
    
        test_t test1; // implicitly defined constructor *not* called (uninitialized)
        test_t test2(); // This is a declaration of a function that returns a test_t
    
        My m1; // explicitly defined constructor is called.
        My m2(); // This is a declaration of a function that returns an My.
    
        return 0;
    }

  11. #11
    Join Date
    Nov 2003
    Posts
    4,118
    First off, int and other fundamental types don't have a constructor, so int i=int(); (which is slightly different from your original example) doesn't invoke a constructor it merely default initialize i, and in the case of fundamental types that means zero-initialize. In other words, there's no difference between:
    Code:
    int i=int();
    and
    Code:
    int i=0;
    or even
    Code:
    int i(0);
    With respect to
    Code:
    M m;
    versus
    Code:
    M m();
    Code:
    M m; //instantiate an object of type M
    is the *only* way to instantiate m. This expression invokes M's default constructor.
    By contrast,
    Code:
    M m();
    is a function declaration/ It declares a function called m that takes no parameters and which returns M by value. Same for
    Code:
    int i(); //function declaration
    which declares a function called i that returns int and takes no arguments.
    Last edited by Danny; 04-30-2010 at 03:42 PM.
    Danny Kalev

Similar Threads

  1. Replies: 3
    Last Post: 11-19-2008, 08:09 AM
  2. Sending mail from VB app
    By Jorge in forum VB Classic
    Replies: 15
    Last Post: 02-13-2007, 01:27 AM
  3. vb smtp help
    By noobz49 in forum VB Classic
    Replies: 4
    Last Post: 09-25-2006, 08:23 AM
  4. Default property problem
    By Paulo Costa in forum VB Classic
    Replies: 2
    Last Post: 07-22-2002, 08:08 AM
  5. Set Default value in ADO Update statement
    By Robert Madrian in forum VB Classic
    Replies: 4
    Last Post: 09-18-2000, 02:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center