Topic: Help with Owl classes ?


Author: jh58273@goodnet.com
Date: 1997/01/03
Raw View
 Is it a new cpp feature that classes can be used as types and
as objects ?   In the examples that come with borland cpp 5.1, most of
the projects start out deriving a new application class from
TApplication.   e.g.,

class myapp: public TApplication { new members ...}

But then for the simplest of examples (HelloWin),  TApplication is
used as an object directly.  e.g.,

int
OwlMain(int /*argc*/, char* /*argv*/ [])
{
  return TApplication("Hello World!").Run();
}


Whats happening?  Is Tapplication now an object ?   Other examples
include all of the help topics on commond dialogs:   A
TFileOpenDialog::TData object is declared.  Then a few lines later,
TFileOpenDialog.Execute() is called.

So,  if someone could tell me what a borland owl "class" is,  or tell
me about the C++ feature that allows something to be both a type and
variable at the sametime, I would be very grateful.

Jason Harris
jh58273@goodnet.com


[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Jay Ernst <ernstjm@bv.com>
Date: 1997/01/03
Raw View
jh58273@goodnet.com wrote:
>
> But then for the simplest of examples (HelloWin),  TApplication is
> used as an object directly.  e.g.,
>
> int
> OwlMain(int /*argc*/, char* /*argv*/ [])
> {
>   return TApplication("Hello World!").Run();
> }
>
> Whats happening?  Is Tapplication now an object ?

An unnamed (aka temporary) instance of a TApplication object is constructed, then the
member function Run() of the unnamed TApplication object is called -- all in one line.
The same thing could be accomplished in two lines:

  TApplication app("Hello World!");
  return app.Run();

Using this syntax is fairly common in C++ in situations where an object is needed only
long enough to call one of its member functions (like above) or to pass into some other
function.

> Other examples include all of the help topics on commond dialogs:   A
> TFileOpenDialog::TData object is declared. Then a few lines later,
> TFileOpenDialog.Execute() is called.

Take a second look at the code -- I imagine that an unnamed instance of the
TFileOpenDialog class is being created when Execute() is called:

  if ( TFileOpenDialog( some_parameters ).Execute() == IDOK ) {
    // do something
  }

>
> So,  if someone could tell me what a borland owl "class" is,  or tell
> me about the C++ feature that allows something to be both a type and
> variable at the sametime, I would be very grateful.
>

Something (a "token") cannot be both a type and an object at the same time.  I think what
is confusing to you is this:  The constructor name is that same as the name of the class,
and the constructor can be called without giving the resulting instance a name.  The key
here is that the constructor cannot be invoked without the parenthesis.

class Bar {
  // ..stuff..
};

void foo(Bar bar);
void foo(int num);


int main()
{
  Bar bar;       // named instance created by call to ctor
  foo( bar );
  foo( Bar() );  // unnamed instance created by call to ctor and passed to foo()
  foo( Bar );    // error -- 'Bar' is a type, not an instance of Bar

  // Analagously:
  int value = 3;
  foo( value );
  foo( 3 );
  foo( int );    // error -- 'int' is a type, not an instance of an integer

}

> Jason Harris
> jh58273@goodnet.com
>
> [ 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         ]
> [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
> [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
> [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]