Prototype or instantiation?

Discussion in 'C++' started by John Doe, Feb 17, 2004.

  1. John Doe

    John Doe Guest

    Please have a look at this thread:

    http://groups.google.com/groups?hl=...15zU3EADHA.2280%40cpmsftngxa06.phx.gbl&rnum=2


    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("wow!");}
    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 Doe, Feb 17, 2004
    #1
    1. Advertising

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


    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
     
    John Harrison, Feb 17, 2004
    #2
    1. Advertising

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


    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
     
    John Harrison, Feb 17, 2004
    #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. Replies:
    1
    Views:
    587
    Salt_Peter
    Dec 25, 2006
  2. June Lee
    Replies:
    2
    Views:
    820
    Jim Cobban
    Apr 13, 2008
  3. Replies:
    9
    Views:
    213
    Thomas 'PointedEars' Lahn
    May 26, 2006
  4. Replies:
    3
    Views:
    278
  5. javascript fish
    Replies:
    0
    Views:
    181
    javascript fish
    Oct 11, 2008
Loading...

Share This Page