invoke non-static function using ::

Discussion in 'C Programming' started by George2, Feb 23, 2008.

  1. George2

    George2

    Joined:
    Feb 12, 2008
    Messages:
    108
    Hello everyone,


    Confused why the following code can compile when we use :: to invoke non-static function. Especially, in the sample below, is it possible to call non-static member if T is not int? Why compiler allows the following code to compile?

    Code:
    #include <iostream>
    
    using namespace std;
    
    template  <class T>
    struct FooTrait {
    public:
    	int foo1();
    };
    
    template<>
    struct FooTrait<int> {
    
    	static int foo1() {cout << "Hello Foo1" << endl; return 0;}
    };
    
    template <class T1, class T2 = FooTrait<T1>>
    struct Foo {
    public:
    	int foo()
    	{
    		T2::foo1(); // possible to call non-static member if T is not int?
    
    		return 0;
    	}
    };
    
     int main()
     {
    	 Foo<int> f;
    	 f.foo();
    	 return 0;
     }
    

    thanks in advance,
    George
     
    George2, Feb 23, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov
    Replies:
    0
    Views:
    1,365
    Victor Bazarov
    Jun 23, 2003
  2. nospam_timur@tabi.org
    Replies:
    4
    Views:
    1,081
    Richard Tobin
    Dec 12, 2006
  3. bhattacharjeesoft@gmail.com

    How to call a non static function from a static function

    bhattacharjeesoft@gmail.com, Aug 6, 2007, in forum: C++
    Replies:
    2
    Views:
    521
    Jonathan Mcdougall
    Aug 6, 2007
  4. dolphin
    Replies:
    3
    Views:
    1,348
    Pete Becker
    Dec 5, 2007
  5. rickman
    Replies:
    5
    Views:
    428
    rickman
    Mar 30, 2013
Loading...

Share This Page