Comments on: This (->) is not only a matter of style. http://phresnel.org/blog/2009/04/this-is-not-only-a-matter-of-style/ D'Oh! (feel free to leave me a mail at "phresnel.gmail@com , swap @ with . for proper mail) Mon, 29 Mar 2010 07:27:12 +0000 hourly 1 http://wordpress.org/?v=3.1.2 By: phresnel http://phresnel.org/blog/2009/04/this-is-not-only-a-matter-of-style/comment-page-1/#comment-5 phresnel Fri, 12 Feb 2010 09:01:21 +0000 http://phresnel.org/blog/?p=99#comment-5 . Hello Hummm, the problem is less about when you call <b>X::vfun()</b>, but when you call a method of a derived class, i.e. <b>Y::fun()</b> (without the "v"), which in turn should call a virtual method. Take this updated snippet: <code>#include <iostream> void vfun() { std::cout << "::vfun()\n"; } template <typename T> struct X { virtual void vfun() const { std::cout << "X::vfun()\n"; } }; template <typename T> struct Y : public X<T> { // During compilation, only the global vfun() // is visible, so this will call ::vfun(). void fun0 () { vfun(); } // // This will explitily call X::vfun() // and inhibit virtual function dispatch. void fun1 () { X<T>::vfun(); } // // Qualifying the call with this makes the call // to vfun() "dependent", as the type of 'this' // depends on X<T>, which is not yet known. // Thus, the compiler will postpone // name-lookup until Phase 2 of template parsing. void fun2 () { this->vfun(); } }; template <typename T> struct Z : Y<T> { virtual void vfun() const { std::cout << "Z::vfun()\n"; } }; int main () { Z<int> z; z.fun0(); z.fun1(); z.fun2(); std::cout << std::flush; }</code> If you are using MSVC, make sure to disable C++ language extensions. .

Hello Hummm,

the problem is less about when you call X::vfun(), but when you call a method of a derived class, i.e. Y::fun() (without the “v”), which in turn should call a virtual method.

Take this updated snippet:

#include
void vfun() {
std::cout < < "::vfun()\n";
}
template struct X {
virtual void vfun() const { std::cout < < "X::vfun()\n"; }
};
template struct Y : public X {
// During compilation, only the global vfun()
// is visible, so this will call ::vfun().
void fun0 () { vfun(); }
//
// This will explitily call X::vfun()
// and inhibit virtual function dispatch.
void fun1 () { X
::vfun(); }
//
// Qualifying the call with this makes the call
// to vfun() "dependent", as the type of 'this'
// depends on X
, which is not yet known.
// Thus, the compiler will postpone
// name-lookup until Phase 2 of template parsing.
void fun2 () { this->vfun(); }
};
template struct Z : Y {
virtual void vfun() const { std::cout < < "Z::vfun()\n"; }
};
int main () {
Z z;
z.fun0();
z.fun1();
z.fun2();
std::cout < < std::flush;
}

If you are using MSVC, make sure to disable C++ language extensions.

]]>
By: Hummm http://phresnel.org/blog/2009/04/this-is-not-only-a-matter-of-style/comment-page-1/#comment-4 Hummm Fri, 12 Feb 2010 06:01:05 +0000 http://phresnel.org/blog/?p=99#comment-4 . I am maybe too noob in C++, but I didn't understand your statement "So, if we now call Z::vfun() through a pointer or reference to struct X, we really call X::vfun(), and not the overriden version in Z." I understood that C++ calls ::vfun() from Y::fun(); However, compiling your example and executing: X * x = &z; x->vfun(); returns "Z::vfun()n". Where is the problem? .

I am maybe too noob in C++, but I didn’t understand your statement “So, if we now call Z::vfun() through a pointer or reference to struct X, we really call X::vfun(), and not the overriden version in Z.”

I understood that C++ calls ::vfun() from Y::fun(); However, compiling your example and executing: X * x = &z; x->vfun(); returns “Z::vfun()n”. Where is the problem?

]]>