Re: How does this work...

From: Mark P (not_at_my.real.email)
Date: 03/01/05


Date: Tue, 01 Mar 2005 01:20:43 GMT

Michael wrote:
> The following code snippet is from Accelerated C++, but I don't
> understand how it can work:
>
> typedef vector<string> Rule;
> typedef vector<Rule> Rule_collection;
> typedef map<string, Rule_collection> Grammar;
>
> // read a grammar from a given input stream
> Grammar read_grammar(istream& in)
> {
> Grammar ret;
> string line;
>
> // read the input
> while (getline(in, line)) {
>
> // `split' the input into words
> vector<string> entry = split(line);
>
> if (!entry.empty())
> // use the category to store the associated rule
> ret[entry[0]].push_back(
> Rule(entry.begin() + 1, entry.end()));
> }
> return ret;
> }
>
>
> The bit I don't understand is this line:
>
> ret[entry[0]].push_back(Rule(entry.begin() + 1, entry.end()));
>
> More specifically, the bil I don't understand is the 'Rule' bit because
> its not an object. Can someone please explain how that code is valid.
>
> Cheers
> Micheal

It's invoking the constructor for Rule, one form of which takes two
iterators as arguments, and creating an unnamed object which is then
inserted into ret[entry[0]]. This form of the constructor copies the
elements between the two iterators to initialize the newly created object.



Relevant Pages

  • Re: How does this work...
    ... Michael wrote: ... >> iterators as arguments, and creating an unnamed object which is then ... That is the form of the constructor being invoked. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: list<class A*> l;
    ... > Because every insertion can throw. ... > And what about insertion with iterators? ... In the copy constructor I have try/catch blocks, ... so it is not safe to call the destructor). ...
    (comp.lang.cpp)
  • Re: Passing Data Between Forms
    ... I was trying to decide if passing the form to the constructor like you ... did would be better than using public static variables, ... >> Michael C. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Convert from std::vector<double> to std::vector<int>
    ... > constructor that takes a pair of iterators. ... template ... value_type operator (const From& f) const ... iterator categories (like forward iterators must return references). ...
    (comp.lang.cpp)
  • Re: How to dynamically create a button?
    ... > this code e.g. in the constructor or better in InitializeComponent) ... Thanks alot, Michael! ...
    (microsoft.public.dotnet.languages.csharp)