which kinds of constructors may be applied during compile time as optimization.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Pooria
    New Member
    • Jul 2010
    • 8

    #1

    which kinds of constructors may be applied during compile time as optimization.

    take two following classes and their constructors as samples:

    Code:
    class One{
     public:
      One(int a,int b):adad1(a),adad2(b){}
     private:
      int adad1;
      int adad2;
    };
    class Two{
     public:
      Two(int input[]){
       for (int i=0;i<10;i++)
        araye[i]=input[i];
      }
     private:
      int araye[10];
    };
    considering objects with static storage duration, I think first constructor can be applied during compile time due to its free function body that allows it to be converted to a constant expression in some cases as an optimization, but I have doubt about second one. anyway is there any rule specifying which kinds of constructors can be applied during compile time ?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    First, constructors are not applied at compile time. They are called only at run time.

    Second, what does static have to do with anything?
    Each of these classes can be used to create static objects.

    Third, what does const have to do with anything?
    Each of these classes can be used to create const objects.

    Are you by any chance thinking of const data members rather than const objects?

    Comment

    • Pooria
      New Member
      • Jul 2010
      • 8

      #3
      Originally posted by weaknessforcats
      weaknessforcats
      First, constructors are not applied at compile time. They are called only at run time.

      Second, what does static have to do with anything?
      Each of these classes can be used to create static objects.

      An implementation is permitted to perform the initialization of an object of namespace scope with static storage duration as a static initialization even if such initialization is not required to be done statically, provided that

      * the dynamic version of the initialization does not change the value of any other object of namespace scope with static storage duration prior to its initialization, and
      * the static version of the initialization produces the same value in the initialized object as would be produced by the dynamic initialization if all objects not required to be initialized statically were initialized dynamically.


      Third, what does const have to do with anything?
      Each of these classes can be used to create const objects.

      Are you by any chance thinking of const data members rather than const objects?
      please go here and take a look around.

      Comment

      Working...