Default Designer



  • class A
    {
      public:
       A():a(0),b(0) {}
       explicit A(int x): a(x), b(0) {}
       A(int x, int y): a(x), b(y) {}
      private:
       int a,b;
    };
    

    and

    class A
    

    {
    public:
    explicit A(int x=0, int y=0): a(x), b(y) {}
    private:
    int a,b;
    };

    Any differences? What better use?



  • These two class announcements are not equivalent.

    In the second announcement of the class, the designer with the specification of the function explicitand this limits the use of this designer in different situations.

    In the first class announcement, only the conversion designer was declared with the specification of the function explicit♪ Which means that the other designers cause you to be invisible.

    That is, the first announcement provides more room for class use.

    Look at the next demonstration program.

    #include <iostream>
    

    struct A
    {
    explicit A( int x = 0, int y = 0 ) : x( x ), y( y ) {}
    int x;
    int y;
    };

    struct B
    {
    B() : x( 0 ), y( 0 ) {}
    explicit B( int x ): x( x ), y( 0 ) {}
    B( int x, int y ): x( x ), y( y ) {}
    int x;
    int y;
    };

    void f( const A &a )
    {
    std::cout << "a.x = " << a.x << ", a.y = " << a.y << std::endl;
    }

    void g( const B &b )
    {
    std::cout << "b.x = " << b.x << ", b.y = " << b.y << std::endl;
    }

    int main()
    {
    // f( {} );
    // f( { 1, 2 } );

    g( {} );
    g( { 1, 2 } );
    

    }

    Its conclusion to the console:

    b.x = 0, b.y = 0
    b.x = 1, b.y = 2

    In this programme, two functional challenges f Commented on, because if they are commented, the compiler will give the message a mistake.

    Another important difference is that one class has only one designer with a given signature and the other has three designers with different signatures.

    Look at another demonstration.

    struct A
    {
    explicit A( int x = 0, int y = 0 ) : x( x ), y( y ) {}
    int x;
    int y;
    };

    struct B
    {
    B() : x( 0 ), y( 0 ) {}
    explicit B( int x ): x( x ), y( 0 ) {}
    B( int x, int y ): x( x ), y( y ) {}
    int x;
    int y;
    };

    struct C
    {
    //friend A::A();
    friend B::B();
    };

    int main()
    {
    }

    Here in class. C You can declare a default designer. B as a friend of the class С♪ But you can't do the same with a default designer. Ato declare him a class friend C♪ 'cause default designer in class A has another signature.

    You'll have to write.

    struct C
    {
    friend A::A( int, int );
    };

    And that might not be what you'd like. I mean, if you, for example, wanted a friend to be a designer, which is purely unjustified.

    I mean, again, when there are individual designers, your possibilities are wider.

    If not designers but functions are considered, the difference is even greater.

    Default arguments do not affect the type of function. So, for example, if you've declared a function as

    void f( int, int = 0 );

    That, despite the argument of default and the fact that you can call her as

    f( value );

    Nevertheless, its type void( int, int )♪ Which in turn means you can't, for example, write.

    void h( void f( int ) )
    {
    f( 10 );
    }

    void f( int x, int y = 0 ) { std::cout << "x = " << x << ", y = " << y << std::endl; }

    //

    h( f );

    as the function parameter h Type void( int ). and the function used as an argument, type void( int, int )

    If you announce two functions instead of one.

    void h( void f( int ) )
    {
    f( 10 );
    }

    void f( int x ) { std::cout << "x = " << x << std::endl; }
    void f( int x, int y ) { std::cout << "x = " << x << ", y = " << y << std::endl; }

    that challenge

    h( f );

    will be correct, as there is a single parameter function.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2