开发者

how to call methods of defined classes from within template class methods

开发者 https://www.devze.com 2023-03-10 16:24 出处:网络
How should I call a method within a defined class from a template class method? Below is my scenario -

How should I call a method within a defined class from a template class method? Below is my scenario -

  1. Template Class

    template <class T>
    class TC {
        void myTemplateMethod() {
            T.myMethod();  //can I call like this ?
        }
    }; 
    
  2. Defined Class

    class tdef {
        void myMethod() { 
            //does something
        }
    };
    
  3. Main

    int main()  {
        TC<tdef> tobj;
        tobj.myTemplateMethod(); //can I call tdef.myMethod() like this?
    }
    

Just to note, that I have debugged a code like this and have found that tdef.myMethod() does not work when called like this. Also are there any chances that some exceptions are not handled while calling tdef.myMethod() 开发者_StackOverflow社区from within Template class method?

-Somnath


That's a non-static member function, so it can only be called on an instance. Templates don't change that fact.

T t;
t.myMethod();

or if the function were static:

T::myMethod();
0

精彩评论

暂无评论...
验证码 换一张
取 消