CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: static function

  1. #1
    Join Date
    Feb 2009
    Posts
    326

    static function

    Hi,

    In the below example, inside the implementation of the static function f1 is there a way to determine whether A::f1() or B::f1() invoked f1 ?

    Code:
    class A
    {
        public:
            static A* f1();
    };
    
    A* A :: f1()
    {
        // Is there a way to identify if A :: f1() or B :: f1() has invoked me ?
        return(new A); //just a dummy return statement for the sake of the example
    
    }
    
    class B : public A
    {};
    
    
    int main()
    {
        A* p1 = A :: f1();
        A* p2 = B :: f1();
    
        delete p1;
        delete p2;
    
        return(0);
    }
    The reason I ask is because I was wondering if f1 function could create an instance of A or B depending on weather A::f1() or B::f1() is called

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: static function

    Quote Originally Posted by Muthuveerappan
    In the below example, inside the implementation of the static function f1 is there a way to determine whether A::f1() or B::f1() invoked f1 ?
    Bad question: neither A::f1() or B::f1() invoked f1, because f1 is A::f1, and there is no recursion involved.

    Quote Originally Posted by Muthuveerappan
    The reason I ask is because I was wondering if f1 function could create an instance of A or B depending on weather A::f1() or B::f1() is called
    Overload f1 by defining B::f1 to return a B*.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Feb 2009
    Posts
    326

    Re: static function

    Thanks Laserlight, you are rite

    I was just going through Cocoa (Objective C's framework) and saw the method alloc which was inherited from the base class (NSObject) and seemed it was something equivalent to static function.

    alloc was creating a new instance for which ever class invoked it.

    I was just wondering if I could draw something parallel in C++.

    I suppose Objective C is a completely different programming language and they have their own semantics and structure and wouldn't be fair to compare with C++.

    Thanks again,
    muthu
    Last edited by Muthuveerappan; August 31st, 2011 at 09:03 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured