[LC++]couple of questions (was: Re: testing)
Carlo Wood
carlo at alinoe.com
Wed Oct 31 02:47:42 UTC 2001
On Tue, Oct 30, 2001 at 06:30:12PM +1030, Mark Phillips wrote:
> Yes, it has been a bit quiet.
>
> Just to liven things up a bit, here's a couple of things I have
> been wondering about:
>
> 1. Does the STL "vector" sit inside the "std" namespace? I thought
yes
> it did, yet I seem to be able to access it without dereferencing with
> "std". Either it doesn't use it, or perhaps I accidentally do a
Use g++-3.x and don't include headers that end on .h.
Ie, use #include <iostream> and not #include <iostream.h>
> "using namespace std" somewhere. I don't think I do though. Is there
> anyway of telling what namespaces are visible at a certain point in
> the code?
>
> 2. I would have thought "this[i].foo()" would mean the same as
> "((*this)[i]).foo()", but it seems it doesn't (at least, according
> to my gcc compiler). It seems to mean "this.foo()" which to me is
> crazy. Can anyone explain!
this is not an array, so you should be using operator[].
this[i] is nonsense.
(*this)[i] would call operator[] of the class that this points to,
but only if you defined it of course.
this.foo() is also nonsense: this is a pointer.
If foo() is a member function of the class that `this' points to,
then you can use: this->foo() or (*this).foo().
Normal is to just use `foo()'.
Example:
----------
#include <iostream>
class Foo {
public:
Foo(void) { }
void foo(void) { std::cout << "this == " << (void*)this << '\n'; }
void bar(void);
};
void
Foo::bar(void)
{
this->foo(); // calls Foo::foo
foo(); // calls Foo::foo
(*this).foo(); // calls Foo::foo
}
int
main()
{
Foo f;
std::cout << "f has `this': " << (void*)&f << '\n';
f.bar();
return 0 ;
}
----------
~>g++-3.0.2 foo.cc
~>a.out
f has `this': 0xbffff677
this == 0xbffff677
this == 0xbffff677
this == 0xbffff677
--
Carlo Wood <carlo at alinoe.com>
More information about the tuxCPProgramming
mailing list