|
-
August 31st, 2011, 12:07 PM
#1
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
-
August 31st, 2011, 12:36 PM
#2
Re: static function
 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.
 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*.
-
August 31st, 2011, 08:52 PM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|