Topic: Is this available in abstract functions?
Author: Cristian Georgescu <Cristian.Georgescu@worldnet.att.net>
Date: 1998/05/10 Raw View
in a member function this returns a pointer to the instance of the
class.
But an abstract class cannot be instantiated, therefore "this" shouldn't
be
available for the functions in such a class.
class A {
public:
virtual void x() = 0;
virtual A*const This() {return this;}
}
However, this is not true, as the code above
seems to be working. Why?...
--
Cristian Georgescu
_________________________________________________
email: CGeorges@lehman.com
tel: (212) 526-3502
_/ _/_/_/_/ Lehman Broders
_/ _/ _/ Equity Finance Systems
_/ _/_/_/_/ World Financial Center
_/ _/ _/ 200 Vesey Street
_/_/_/ _/_/_/_/ New York, NY 10285.
_________________________________________________
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ 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: "Andrew Gayter" <Andrew.Gayter@btinternet.com>
Date: 1998/05/11 Raw View
When classes are instantiated they are allocated contiguous blocks of memory.
The this pointer can refer to any data or function within that block by
calculation of an offset from the base block address.
The this pointer would be sizeof(class heirarchy)
Andrew
Cristian Georgescu wrote in message <6j1u4b$lut@netlab.cs.rpi.edu>...
>in a member function this returns a pointer to the instance of the
>class.
>But an abstract class cannot be instantiated, therefore "this" shouldn't
>be available for the functions in such a class.
>
>class A {
>public:
> virtual void x() = 0;
> virtual A*const This() {return this;}
>}
>
>However, this is not true, as the code above
>seems to be working. Why?...
[excess quoting deleted --hlh]
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ 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: dietmar.kuehl@claas-solutions.de
Date: 1998/05/11 Raw View
Hi,
In article <6j1u4b$lut@netlab.cs.rpi.edu>,
Cristian.Georgescu@worldnet.att.net wrote:
>
> in a member function this returns a pointer to the instance of the
> class.
> But an abstract class cannot be instantiated, therefore "this" shouldn't
> be available for the functions in such a class.
There is no problem with instantiation here: 'this' is a pointer to the object
on which the method is invoked. Although the static type of the object
pointed to by 'this' may be an abstract class, the dynamic type will be a
concrete class. "static type" is the type which can be checked by the
compiler. It may be different from the actual type if the object is accessed
using a reference or a pointer. "dynamic type" is the actual type of the
object. It will always be a concrete class.
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ 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: "Phlip" <tegan@deltanet.com>
Date: 1998/05/11 Raw View
Cristian Georgescu wrote:
>in a member function this returns a pointer to the instance of the
>class.
>But an abstract class cannot be instantiated, therefore "this" shouldn't
>be
>available for the functions in such a class.
>
>class A {
>public:
> virtual void x() = 0;
> virtual A*const This() {return this;}
>}
>
>However, this is not true, as the code above
>seems to be working. Why?...
An abstract class would be useless unless you could point or refer to it.
Here is a function that uses B or C derived from your A:
void polymorph (A &a);
B b;
C c;
polymorph (b);
polymorph (c);
Inside B or C lives a "sub-object" of type A. The phrase "cannot be
instantiated" is incomplete - you meant to say "cannot be instantiated as a
complete object".
-- Phlip
======= http://users.deltanet.com/~tegan/home.html =======
Read Bruno the Bandit! at:
http://www.geocities.com/Area51/Zone/5167
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: Vaclav Barta <vbar@comp.cz>
Date: 1998/05/11 Raw View
Cristian Georgescu wrote:
>
> in a member function this returns a pointer to the instance of the
> class.
> But an abstract class cannot be instantiated, therefore "this" shouldn't
> be
> available for the functions in such a class.
No. An instance of derived class is an instance of base class - and
that's the "this" you're working with in member functions of the base
class. Note that to call a (non-static) member function, you must have
an instance.
> class A {
> public:
> virtual void x() = 0;
> virtual A*const This() {return this;}
> }
The above compiles OK - but this will not:
A *a = A::This();
To understand C++, it is IMHO helpful to know the actual implementation
of classes, instances,inheritance, virtual functions etc. - even if
it's not, strictly speaking, part of language definition. S. Lippman
calls it "the C++ object model" and describes it in a book of the same
name, which I heartily recommend.
Bye
Vasek
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: "Piet Van Vlierberghe" <pieter.vanvlierberghe@lms.be>
Date: 1998/05/11 Raw View
Cristian Georgescu <Cristian.Georgescu@worldnet.att.net> wrote in article
<6j1u4b$lut@netlab.cs.rpi.edu>...
> in a member function this returns a pointer to the instance of the
> class.
> But an abstract class cannot be instantiated, therefore "this" shouldn't
> be
> available for the functions in such a class.
>
The actual type of 'this' might be a nonabstract subclass of A. What is
wrong with that?
Actually, the fact that you cannot create an object of an abstract class
*guarantees* that 'this'
will be a nonabstract subclass of A.
> class A {
> public:
> virtual void x() = 0;
> virtual A*const This() {return this;}
> }
>
[ 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: jkanze@otelo.ibmmail.com
Date: 1998/05/12 Raw View
In article <6j1u4b$lut@netlab.cs.rpi.edu>,
Cristian.Georgescu@worldnet.att.net wrote:
>
> in a member function this returns a pointer to the instance of the
> class.
> But an abstract class cannot be instantiated, therefore "this" shouldn't
> be
> available for the functions in such a class.
I don't see the connection. You cannot instantiate an abstract class,
but you can instantiate its subclasses, and you can have pointers to
the abstract base class of these subclasses. Having an abstract
base whose constructor registers the object somewhere is a frequent
idiom; the constructor calls the registry function with its this pointer.
--
James Kanze +33 (0)1 39 23 84 71 mailto: kanze@gabi-soft.fr
+49 (0)69 66 45 33 10 mailto: jkanze@otelo.ibmmail.com
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
Conseils en informatique oriente objet --
-- Beratung in objektorientierter Datenverarbeitung
-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ 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: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1998/05/14 Raw View
Cristian Georgescu wrote:
>
> in a member function this returns a pointer to the instance of the
> class.
> But an abstract class cannot be instantiated, therefore "this"
> shouldn't be
> available for the functions in such a class.
>
> class A {
> public:
> virtual void x() = 0;
> virtual A*const This() {return this;}
> }
If you derive from A, but don't override This, then the derived class
will inherit the definition of This, thus making it callable.
--
Ciao,
Paul
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
---
[ 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 ]