Topic: What does this do?


Author: "Paul Coleman" <sales@digi-die.freeserve.co.uk>
Date: 1999/05/21
Raw View
Does anybody know what the following line (the one with the comment) does?
----------------------------------------------------------------------------
-------------------------------------------------------------------
#include <iostream.h>

class CTest {
  int a;
public:
  CTest() {i = 0;}
  void display() {cout << i << endl;}
};

void main()
{
  CTest obj1;            // this defines an object of CTest
  obj1.display();        // okay calls display on obj1

  CTest obj2();        // What does this do????????
  obj2.display();        // compiler error - obj2 not declared
}
----------------------------------------------------------------------------
--------------------------------------------------------------------
I have tried this code on both Borland and Microsoft compilers and both
produce the same results. In both cases the compilers produce no code for
the line CTest obj2(); Therefore obj2 does not exist and cannot have the
display routine executed on it.

I made this mistake accidentally whilst coding on another project which is
more complicated than this - the above is only to illustrate the problem.
Neither compiler produces an error not a warning about this line, they just
don't produce any code for it??

I have looked in all the books I have but cannot find any examples of this
mistake? Does anybody have any ideas as to what this statement does?

Thanks Paul Coleman.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/05/21
Raw View
Paul Coleman wrote:
>
> Does anybody know what the following line (the one with the comment) does?
> ----------------------------------------------------------------------------
> -------------------------------------------------------------------
> #include <iostream.h>
>
> class CTest {
>   int a;
> public:
>   CTest() {i = 0;}
>   void display() {cout << i << endl;}
> };
>
> void main()
> {
>   CTest obj1;            // this defines an object of CTest
>   obj1.display();        // okay calls display on obj1
>
>   CTest obj2();        // What does this do????????

It declares a function named obj2, with no arguments, returning a CTest
object.

>   obj2.display();        // compiler error - obj2 not declared

It should say, obj2 not defined; the above was the declaration for it.

> }
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Dag Henriksson <dag.henriksson@quidsoft.se>
Date: 1999/05/22
Raw View
Paul Coleman wrote:

> Does anybody know what the following line (the one with the comment) does?
> ----------------------------------------------------------------------------
> -------------------------------------------------------------------
> #include <iostream.h>
>
> class CTest {
>   int a;
> public:
>   CTest() {i = 0;}
>   void display() {cout << i << endl;}
> };
>
> void main()
> {
>   CTest obj1;            // this defines an object of CTest
>   obj1.display();        // okay calls display on obj1
>
>   CTest obj2();        // What does this do????????
>   obj2.display();        // compiler error - obj2 not declared
> }

It declares a function obj2 that takes no argument and returns a CTest.

-- Dag Henriksson
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]