Prototype or instantiation?

J

John Doe

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
 
J

John Harrison

....
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
 
J

John Harrison

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top