Index:
[thread]
[date]
[subject]
[author]
From: Garrett Mitchener <wgm2@duke.edu>
To :
Date: 01 Apr 1999 12:59:22 -0500
Re: this
"Luke Palmer" <lop@duke.edu> writes:
> in reading through all this and other java code....
>
> the "this" keyword pops up fairly often, probably more so than i have seen
> in c++. i know that the definition of "this" is that it is a pointer to the
> current object. unfortunately, i have never received a decent explanation
> of what the significance of this is, what it actually does, or how it can be
> used. also, since the concept of pointers is very different in java, how is
> "this" different or the same? thanks.
> -luke
The idea of "this" in C++ is that all non-static member functions need
to be able to access the data fields of the object they work on. So
when you do
class Foo
{
public:
void DoStuff( int n );
private:
int myNumber = 0;
};
the compiler turns that into
void DoStuff( Foo * this, int n )
automatically for you. In the definition
void Foo::DoStuff( int n )
{
myNumber += n;
}
gets turned into
void Foo::DoStuff( Foo * this, int n )
{
this->myNumber += n;
}
NOTE: Do *NOT* ever declare the "this" parameter in C++ or in Java,
since the compiler does it for you always automatically. You may use
"this", but you never declare it.
The most common use of "this" is something like:
class Foo : public Gizmo
{
public:
void DoStuff( Bar * b )
{
b->AddNewGizmo( this ); // Pass myself to another function...
}
Foo & operator=( Foo & other )
{
if( this == &other ) // Make sure I don't clobber myself in assignment
return;
else
...
}
}
You do similar stuff in Java, and this works basically the same as any
other variable. Just never assign "this = ..." in either language
because that's just silly. (Think for a moment and you can probably
guess why...)
In Java, there is also a variable named "super" (I think this is
right-- someone double check me) which is used to do a special form of
function overriding:
public class Foo
{
public void doStuff( int n )
{...}
}
public class Bar extends Foo
{
public void doStuff( int n )
{
System.out.println( "Calling Bar.doStuff" );
super.doStuff( n );
}
}
This allows you to use methods from the base class in the derived
class.
PS: Other languages do different things. In Smalltalk, you have
"self" instead of "this" but you also have "super". In Python, you
have to declare "self" manually as a formal argument because there is
no reserved word for it (and you could call it "this" if you wanted,
but "self" is traditional Python style). In C, you can do
object-oriented programming with structs, although it can get ugly,
and then you have to declare all your arguments including the object
you're acting on and you can call it whatever you want.
-- Garrett :-)
PPS: Considering what day it is, and the release date in the note you
tacked on below, I find it slightly "suspicious", wink wink...
> For those that are interested: This is sick. They really are going to take
> over the world someday.
>
> --snip--
>
> Microsoft Announces MS-Linux
>
> The flexible "grassroots" operating system, now with the quality and
> enterprise-level service and support customers expect from Microsoft
>
> REDMOND, Wash., April 1 /PRNewswire/ -- Microsoft Corp. today
> announced the forthcoming release of MS-Linux(R), a new version of the
> well-known Linux(R) operating system designed for the enterprise.
>
> "Prior to the development of MS-Linux, enterprise customers wouldn't
> dream of deploying mission-critical applications on an unsupported
> operating system," said Jim Allchin, senior vice president of Microsoft's
> personal and business systems group. "All that has now changed."
>
> Maximum compatibility and performance
> While other versions of Linux offer a variety of immature and largely
> incompatible windowing environments, MS-Linux includes an implementation
> of the familiar Microsoft(R) Windows(R) desktop, minimizing corporate
> training costs and allowing the hundreds of millions of Windows users
> worldwide to be productive right away.
>
> MS-Linux provides a complete implementation of the Win32(R) API,
> enabling many of the thousands of applications written for the Microsoft
> Windows 95, Windows 98, Windows NT(R), and Windows 2000 operating systems
> to run unchanged on MS-Linux. Microsoft will offer support for future
> releases of the Microsoft Office suite of productivity applications and
> the BackOffice family of server products running on MS-Linux. In addition,
> many existing MS-DOS(R) and SCO Unix programs will run on MS-Linux without
> modification. Benchmark tests performed by independent laboratories have
> shown that MS-Linux is the world's fastest Windows file server.
>
> A groundswell of support from industry
> A number of leading development tool vendors will announce support for
> MS-Linux, including Bristol Technology Inc., Compuware Corp., Cygnus
> Solutions, Fujitsu Microelectronics Inc., Intel Corp., Metrowerks Inc.,
> Rational Software Corp., Sybase Inc., and Tower Technology.
>
> "The new Linux distribution from Microsoft gives the operating system
> the credibility it needs to gain a foothold in organizations where it has
> never gone before," said Jon Hall, Executive Director of Linux
> International, a non-profit Linux advocacy organization. Microsoft will
> become a corporate member of Linux International, and will make a
> substantial donation of funds and equipment to the organization. "Linux
> International looks forward to working with Microsoft for the benefit of
> current and future Linux users," added Hall.
>
> According to VA Research President and CEO Larry Augustin, "VA
> Research is delighted that Microsoft is entering the fast-growing market
> forcomputers powered by Linux." VA Research has announced plans to bundle
> MS-Linux and selected Microsoft Office and BackOffice products with its
> powerful server, workstation, and laptop computers. VA Research has also
> agreed to make Microsoft its exclusive supplier of Linux-based operating
> systems going forward. "Having the Office and BackOffice suites running on
> computers from VA Research will give us a huge advantage in the
> marketplace," said Augustin.
>
> Microsoft has engaged well-known author and programmer Eric S. Raymond
> to advise the company on certain intellectual property issues related to
> Linux. Raymond's paper "The Cathedral of the Bizarre" was a key factor in
> Microsoft's decision to develop a Linux product. "While Microsoft will not
> immediately release the source code for its version of Linux, the company
> has promised to do so eventually," said Raymond. "That's good enough for
> me."
>
> "The Linux community and Microsoft are a great fit," says Linus
> Torvalds, creator of Linux. "After all, we both have the same goal, which
> is total world domination."
>
> Separately, Microsoft has announced the opening of a new recruiting
> center in Research Triangle Park, North Carolina. "Microsoft is always
> looking for talented individuals to join our team," said Mike Murray,
> Microsoft's vice president of human resources and administration. "We
> often recruit in communities that are expected to have significant numbers
> of folks looking for work in the near future."
>
> Pricing and availability
> Microsoft expects to release English and international versions of
> MS-Linux Version 3.1 for Intel- and RISC-based computers on or before
> April 1, 2000. Pricing has not been announced.
>
> About Microsoft
> Founded in 1975, Microsoft (Nasdaq: MSFT) is the worldwide leader in
> software for personal computers. The company offers a wide range of
> products and services, each designed with the mission of making it easier
> and more enjoyable to take advantage of people using personal computers
> every day.
>
> Microsoft, Windows, Windows NT, Win32, BackOffice, MS-Linux, and Linux
> are either registered trademarks or trademarks of Microsoft Corp. in the
> United States and/or other countries. Other product and company names
> herein may be trademarks of their respective owners.
Index:
[thread]
[date]
[subject]
[author]