stl container as default arguments - how?

Discussion in 'C++' started by FBergemann@web.de, Nov 2, 2006.

  1. Guest

    Hi,

    i wanted to merge two constructors

    explicit Tokenizer (const string& token);
    explicit Tokenizer (const string& token, const list<string>&
    elements);

    into one

    explicit Tokenizer (const string& token, const list<string>&
    elements = const list<string>& ());

    But this doesn't compile.
    What's the correct syntax?

    - Thanks!

    Frank
     
    , Nov 2, 2006
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > Hi,
    >
    > i wanted to merge two constructors
    >
    > explicit Tokenizer (const string& token);
    > explicit Tokenizer (const string& token, const list<string>&
    > elements);
    >
    > into one
    >
    > explicit Tokenizer (const string& token, const list<string>&
    > elements = const list<string>& ());
    >
    > But this doesn't compile.
    > What's the correct syntax?


    You can't default-initialize a reference.

    explicit Tokenizer (const string& token,
    const list<string>& elements = list<string> ());

    Note that this will create a temporary empty list whenever the default
    argument is used.
     
    Rolf Magnus, Nov 2, 2006
    #2
    1. Advertising

  3. Guest

    Rolf Magnus schrieb:
    [...]
    thanks for the hint - but you're right:

    > Note that this will create a temporary empty list whenever the default
    > argument is used.


    that's a reason to hold on two constructors for it

    Regards!

    Frank
     
    , Nov 3, 2006
    #3
    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. Maitre Bart
    Replies:
    2
    Views:
    539
    Maitre Bart
    Feb 11, 2004
  2. Edward Diener
    Replies:
    14
    Views:
    5,084
    Josiah Carlson
    Apr 6, 2004
  3. Replies:
    4
    Views:
    826
    Daniel T.
    Feb 16, 2006
  4. wolverine
    Replies:
    2
    Views:
    470
    Marcus Kwok
    Jul 24, 2006
  5. tutmann
    Replies:
    4
    Views:
    454
Loading...

Share This Page