Prototype or instantiation?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Doe

    Prototype or instantiation?

    Please have a look at this thread:




    When I read it I couldn't believe that! I mean.... for years I have done
    things like this for instantiating objects:

    --------------------------------------
    class CL
    {
    public:
    CL (std::string s) {i_=s.length(); }
    std::string get_a_string() {return std::string("wo w!");}
    protected:
    int i_;
    };



    .....
    int main(....)
    {
    CL cl(std::string( "whatever") );
    std::cout << cl.get_a_string ();
    .....

    }
    --------------------------------------

    with VC++6 and it would work! Today I have tried again with VC7.1 and it
    still works!

    So what is the above googlegroups thread speaking about? I can't see the
    difference between the code posted there and my code, which DOES
    instantiate the object.

    Thanks in advance
  • John Harrison

    #2
    Re: Prototype or instantiation?

    >[color=blue]
    > ....
    > int main(....)
    > {
    > CL cl(std::string( "whatever") );
    > std::cout << cl.get_a_string ();
    > ....
    >
    > }[/color]

    That works because "whatever" cannot be mistaken for a parameter name. But
    try this

    CL cl(std::string( whatever));

    That declares a function called cl, returning a CL type object and taking a
    single parameter of type std::string.

    The parameter name in this prototype is whatever and it has a superfluous
    but legal pair of brackets around it.

    john


    Comment

    • John Harrison

      #3
      Re: Prototype or instantiation?

      >[color=blue]
      > The parameter name in this prototype is whatever and it has a superfluous
      > but legal pair of brackets around it.
      >[/color]

      Maybe you'll find it easier to grasp if you realise that

      int (a);

      is a perfectly legal way to declare the variable a.

      john


      Comment

      Working...