constructors

This is a discussion on constructors within the C++ Programming forums, part of the General Programming Boards category; i have read that there is type of constructor in c++ called the dynamicconstructor. Does anyone know what that is?I ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    1

    Post constructors

    i have read that there is type of constructor in c++ called the dynamicconstructor.
    Does anyone know what that is?I could'nt find any info on it any where

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    What you read could possibly have been refering to the 'new' operator. Although it is not a constructor, it is used in conjuction with a constructor:

    MyClass* ptr = new MyClass(52, "hello world");

    to dynamically allocate memory for objects.

    Post what you read.
    Last edited by 7stud; 06-24-2005 at 03:50 AM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Never heard of a "dynamic constructor".

    There are some people who claim incorrectly that a constructor is needed if an object is created dynamically (i.e. with operator new). Creating with operator new is sometimes referred to as "dynamic construction".

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    There are some people who claim incorrectly that a constructor is needed if an object is created dynamically
    I'm wondering how an object can be created without a constructor?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    The compiler generated constructor is used. A user-defined constructor is what is not needed.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Did you mean virtual constructor??
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212
    From Inside the C++ Object Model by Stanley B. Lippman

    2.1 Default Constructor Construction

    The C++ Annotated Reference Manual (ARM) [ELLIS90] (Section 12.1) tells us that "default constructors…are generated (by the compiler) where needed…."


    When is a default constructor synthesized, then? Only when the implementation needs it. Moreover, the synthesized constructor performs only those activities required by the implementation.


    The Standard has refined the discussion in the ARM, although the behavior, in practice, remains the same. The Standard states [ISO-C++95] (also Section 12.1) the following:

    If there is no user-declared constructor for class X, a default constructor is implicitly declared…. A constructor is trivial if it is an implicitly declared default constructor….

    The standard then goes on to iterate the conditions under which the implicit default constructor is considered trivial. A nontrivial default constructor is one that in the ARM's terminology is needed by the implementation and, if necessary, is synthesized by the compiler.


    There are four characteristics of a class under which the compiler needs to synthesize a default constructor for classes that declare no constructor at all. The Standard refers to these as implicit, nontrivial default constructors. The synthesized constructor fulfills only an implementation need. It does this by invoking member object or base class default constructors or initializing the virtual function or virtual base class mechanism for each object. Classes that do not exhibit these characteristics and that declare no constructor at all are said to have implicit, trivial default constructors. In practice, these trivial default constructors are not synthesized.

    Within the synthesized default constructor, only the base class subobjects and member class objects are initialized. All other nonstatic data members, such as integers, pointers to integers, arrays of integers, and so on, are not initialized. These initializations are needs of the program, not of the implementation. If there is a program need for a default constructor, such as initializing a pointer to 0, it is the programmer's responsibility to provide it in the course of the class implementation.

    Programmers new to C++ often have two common misunderstandings:

    That a default constructor is synthesized for every class that does not define one

    That the compiler-synthesized default constructor provides explicit default initializers for each data member declared within the class

    As you have seen, neither of these is true.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Quote Originally Posted by Daved
    The compiler generated constructor is used. A user-defined constructor is what is not needed.
    Sort of.

    The compiler generated default constructor (if any) only default initialises the members and bases of a class. For a class X, with no base class, the body of a compiler generated default constructor would be effectively;
    Code:
    X::X()
    {
    }
    which most modern compilers will optimise away (i.e. not even attempt to call it).

    C++ was designed with backward compatibility to C in mind, and C did not support constructors or destructors at all. A consequence of this is that, when working with basic POD (plain old data) types that will work with C, the programmer is not allowed to write constructors, and the compiler doesn't need to generate them either (and the smarter compilers often don't).

    Also, giving a class a user-defined constructor (at least ones that the compiler wouldn't generate by itself) suppresses generation of a default constructor.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays and Constructors
    By Verdagon in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:20 PM
  2. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  3. constructors, arrays, and new
    By Thantos in forum C++ Programming
    Replies: 6
    Last Post: 05-30-2004, 06:21 PM
  4. constructors in classes
    By Kenman in forum C++ Programming
    Replies: 16
    Last Post: 07-28-2003, 07:35 AM
  5. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 09:16 AM

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