Parameterless Constructor - Struct

This is a discussion on Parameterless Constructor - Struct within the C++ Programming forums, part of the General Programming Boards category; Referring to - http://www.codeproject.com/csharp/structs_in_csharp.asp Although the CLR allows it, C# does not allow structs to have a default parameterless constructor. ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Parameterless Constructor - Struct

    Referring to - http://www.codeproject.com/csharp/structs_in_csharp.asp

    Although the CLR allows it, C# does not allow structs to have a default parameterless constructor. The reason is that, for a value type, compilers by default neither generate a default constructor, nor do they generate a call to the default constructor. So, even if you happened to define a default constructor, it will not be called and that will only confuse you. To avoid such problems, the C# compiler disallows definition of a default constructor by the user.

    this is a C# tutorial segment, but since C++ too supports structs, so I was wondering if the same thing holds for C++

    i.e. does C++ also does not provide default parameterless constructor? and are these constructors, if i explicitly define them in my definition be ever called?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Why would you expect a rule in C# to apply to C++? Sure, in C++/CLI (which is a .NET language) you can define a value class that has these semantics, but in standard C++, the only difference between a structure and a class is the default access (public for structures, private for classes).
    My best code is written with the delete key.

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    If you do not declare/define any constructor, the compiler will create one for you and call it when an object of that type is created.

    If you declare/define a parameterized constructor, no default (parameterless) one will be created and any creation of the object must be made through the parameterized one defined. Though it doesn't prohibit a parameterless one being defined also.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Few More Questions!

    Structs in C++ are stored in Stack/Heap?

    What will be the default value of my struct type?

    Assignment of struct types on to another struct type will copy values or reference?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    yes
    types don't have values.
    values

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    99
    Quote Originally Posted by robwhit View Post
    yes
    so, are they stored in Heap or Stack???

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    Quote Originally Posted by vb.bajpai View Post
    Structs in C++ are stored in Stack/Heap?
    As with everyting in C++, depends how you create it. You can allocates structs on either the heap or the stack.

    What will be the default value of my struct type?
    Uninitialized stack variables will contain garbage values. Initialized stucts will defaultly call the default constructors of each of there members. The default constructors for primitive types cause zero initialization.

    Assignment of struct types on to another struct type will copy values or reference?
    Assigning to a value will make a copy. Initializing a reference will make a reference. It is also possible to assign pointers, which are special types for manipulating data by reference. Again, structs are not special in this respect -- all types can be manipulated by all three ways.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    stack in scope, heap with new.
    Code:
    void func()
    {
        df gb; //stack
        df *dfp = new df; //heap
        delete dfp;
        return;
    }

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Quote Originally Posted by King Mir View Post
    Uninitialized stack variables will contain garbage values. Initialized stucts will defaultly call the default constructors of each of there members. The default constructors for primitive types cause zero initialization.
    Not true. The initialization rules for primitives are confusing and hard to describe. An explicit call to a primitive's default constructor (e.g. int()) causes zero-initialization, but the compiler-generated default constructor doesn't initialize primitives at all.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If I define a vector of ints as follows:
    Code:
      std::vector<int> v(100);
    are the elements of v initialized to zero, or not? I've read that in general the default constructor is called for each element, but couldn't figure out whether that applied to a primitive type.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,588
    are the elements of v initialized to zero, or not? I've read that in general the default constructor is called for each element, but couldn't figure out whether that applied to a primitive type.
    They are initialised to zero since the default constructor is called explicitly.
    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

  12. #12
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by robatino View Post
    If I define a vector of ints as follows:
    Code:
      std::vector<int> v(100);
    are the elements of v initialized to zero, or not? I've read that in general the default constructor is called for each element, but couldn't figure out whether that applied to a primitive type.
    Yes, they will be zero

    ** - too slow :-(

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    They will be zero, but that's because the line

    Code:
    std::vector<int> v(100)
    is equivalent (by making explicit the default argument) to

    Code:
    std::vector<int> v(100, int())
    This effectively passes 0 as the second argument to the constructor, and all elements are initialized to this value.


    A vector is a much higher-level case anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

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