using constructors for primitive types?!

Discussion in 'C++' started by Gaijinco, Jun 18, 2007.

  1. Gaijinco

    Gaijinco Guest

    I have used before:

    class A
    {

    };

    operator& operator<<(operator& out, A& a);

    cout << A();

    I used thinking this spent fewer memory than doing something like:

    A a;
    cout << a;

    Now I tried this and it worked:

    cout << int(0);

    Is this compiler-dependent or is a language feature?
    Gaijinco, Jun 18, 2007
    #1
    1. Advertising

  2. Gaijinco

    Kai-Uwe Bux Guest

    Gaijinco wrote:

    > I have used before:
    >
    > class A
    > {
    >
    > };
    >
    > operator& operator<<(operator& out, A& a);
    >
    > cout << A();
    >
    > I used thinking this spent fewer memory than doing something like:
    >
    > A a;
    > cout << a;


    That may or may not use more memory. The compiler is free to optimize away
    the variable unless it would change the observable behavior of the program.


    > Now I tried this and it worked:
    >
    > cout << int(0);
    >
    > Is this compiler-dependent or is a language feature?


    It is a language feature. It is important, e.g., in templated code where it
    allows one to treat built-in types and user defined classes uniformly.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Jun 18, 2007
    #2
    1. Advertising

  3. Gaijinco wrote:
    > I have used before:
    >
    > class A
    > {
    >
    > };
    >
    > operator& operator<<(operator& out, A& a);


    What is operator (the type) here? You can't have a type named so. Did you
    mean std::eek:stream?
    For outputting it would be wise to make the reference const:

    std::eek:stream& operator<<(std::eek:stream& out, const A& a);

    >
    > cout << A();
    >
    > I used thinking this spent fewer memory than doing something like:
    >
    > A a;
    > cout << a;


    It would be the same, I guess. Why should it use more or less memory?

    > Now I tried this and it worked:
    >
    > cout << int(0);
    >
    > Is this compiler-dependent or is a language feature?


    What is compiler-dependent?

    cout can output an integer, of course. It's like:

    cout << 42;

    --
    Thomas
    http://www.netmeister.org/news/learn2quote.html
    Thomas J. Gritzan, Jun 18, 2007
    #3
  4. Gaijinco

    Ron Natalie Guest

    Gaijinco wrote:

    > Now I tried this and it worked:
    >
    > cout << int(0);
    >
    > Is this compiler-dependent or is a language feature?
    >


    That is not a call to the int constructor. Int doesn't
    have a constructor. It is an explicit conversion to
    int. It is (by definition in the language) exactly the
    same as:
    cout << (int) 0;
    Ron Natalie, Jun 18, 2007
    #4
  5. Gaijinco

    Gaijinco Guest

    > Int doesn't
    > have a constructor. It is an explicit conversion to
    > int. It is (by definition in the language) exactly the
    > same as:
    > cout << (int) 0;


    And what's the case when you use:

    class A
    {
    int x;
    A(int z): x(z){};
    }

    Thanks.
    Gaijinco, Jun 18, 2007
    #5
  6. Gaijinco

    Sarath Guest

    Sarath, Jun 18, 2007
    #6
  7. Gaijinco

    Ian Collins Guest

    Gaijinco wrote:
    >> Int doesn't
    >> have a constructor. It is an explicit conversion to
    >> int. It is (by definition in the language) exactly the
    >> same as:
    >> cout << (int) 0;

    >
    > And what's the case when you use:
    >
    > class A
    > {
    > int x;
    > A(int z): x(z){};


    That's an initialiser, different beast.

    --
    Ian Collins.
    Ian Collins, Jun 18, 2007
    #7
  8. Gaijinco

    James Kanze Guest

    On Jun 18, 2:10 am, Ron Natalie <> wrote:
    > Gaijinco wrote:
    > > Now I tried this and it worked:


    > > cout << int(0);


    > > Is this compiler-dependent or is a language feature?


    > That is not a call to the int constructor. Int doesn't
    > have a constructor. It is an explicit conversion to
    > int. It is (by definition in the language) exactly the
    > same as:
    > cout << (int) 0;


    That's true, but:
    cout << A(0) ;
    is also the same as:
    cout << (A)0 ;
    and
    cout << static_cast< A >( 0 ) ;

    They're all conversions (according to the standard). The only
    real difference is that "type(arg_list)" allows any number of
    args (including 0), where as the two other syntaxes only work
    with exactly one argument, and that "type(arg_list)" requires
    that the type be a single token or a qualified name, where as
    the two other syntaxes accept more complicated type names (like
    "unsigned long"). But the standard still qualifies all as "type
    conversions" (and defines the semantics of A(arg) in terms of
    static_cast when there is a single argument).

    --
    James Kanze (GABI Software, from CAI) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 18, 2007
    #8
  9. Gaijinco

    Ron Natalie Guest

    Gaijinco wrote:
    >> Int doesn't
    >> have a constructor. It is an explicit conversion to
    >> int. It is (by definition in the language) exactly the
    >> same as:
    >> cout << (int) 0;

    >
    > And what's the case when you use:
    >
    > class A
    > {
    > int x;
    > A(int z): x(z){};
    > }


    Same thing. Except that classes have constructors
    that are called by the implementation to initialize
    them.
    Ron Natalie, Jun 18, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave Rudolf
    Replies:
    12
    Views:
    8,271
    Martijn Lievaart
    Feb 6, 2004
  2. Replies:
    7
    Views:
    599
    Victor Bazarov
    May 9, 2005
  3. Jeremy Smith
    Replies:
    2
    Views:
    586
    Jeremy Smith
    Aug 3, 2006
  4. Jess
    Replies:
    5
    Views:
    593
    Ron Natalie
    Jun 7, 2007
  5. Daniel Pitts
    Replies:
    7
    Views:
    470
Loading...

Share This Page