static function may not be inherited, it is not in scope of derived class? WHY?
-
Tuesday, February 03, 2009 5:58 AMHi Friends,
static function may not be inherited, it is not in scope of derived class? WHY?
can somebody let me know?
Thanks in advance
All Replies
-
Tuesday, February 03, 2009 10:07 AMModeratorPost a snippet of the code that produces this error.
Hans Passant. -
Tuesday, February 03, 2009 11:32 AM
please check below snippet of code.. it is working.. Is it correct code..
does it mean static member function are inheritable.
Thanks in advance..
#include<iostream.h>
class base
{
static int a;
public:
static check(int i)
{
a = i;
}
void get()
{
cout<<"i's current value is: "<< a <<endl;
}
};int base::a=0;
class derived: public base
{
};int main()
{
//base b;
derived d;
d.check(6);
//b.get();
d.get();
return 0;
} -
Tuesday, February 03, 2009 11:43 AMModeratorCalling a static method as though it is an instance method doesn't make a lot of sense, although C++ allows it. Write it like this:
int main()
{
derived d;
base::check(6); // Or derived::check(6)
d.get();
return 0;
}
Hans Passant. -
Tuesday, February 03, 2009 11:54 AM
Hello Hans,
The snippet of code you suggested me is working.
int main()
{
derived::check(6);
derived d;
d.get();
return 0;
}
does this mean static member function are inheritable. What member are not inheritable like constructor, destructor and friend function? Please make me correct if I a worng.
Thanks for your help and cooperation. -
Tuesday, February 03, 2009 12:40 PMModerator
Hmya, it is a matter of semantics. Inheritance applies to classes. Talking about inheriting methods puts you on a slipperly slope. One possible definition for that is "capable of being overridden in a derived class". From that point of view, a static method in a base class is not inherited, you can't override it. The methods that you must override are the copy constructor and the assignment operator. The methods you probably should override are the constructor and destructor.
Hans Passant.- Marked As Answer by Nancy ShaoModerator Monday, February 09, 2009 3:49 AM
-
Tuesday, February 03, 2009 1:36 PMAs you said "capable of being overridden in a derived class".
by this defination we can say that ..
below members of base class would not be a part of inheritance.
(a) CTOR/DTOR
(b)copy CTOR/ Assignment operator
(c) static member functions
Please confirm the same.
Thanks in advance. -
Wednesday, February 04, 2009 5:49 AM
As you said "capable of being overridden" is can be called as "Inheritable"
below static function is overridden
Please explain I could not understand
#include<iostream.h>
class base
{
public:
static check(int i)
{
a = i;
}
void get()
{
cout<<"i's current value is: "<< a <<endl;
}
static int a;
};int base::a=0;
class derived: public base
{
public:
static check(int i)
{
a = i;
}
};int main()
{
derived::check(6);
derived d;
// d.check(6);d.get();
return 0;
} -
Friday, February 06, 2009 10:13 AMModerator
Hi vaibhavsaxena17,
We can inherit static functions from class but you can't override the static function. This is because static methods don't have an entry in the vtable, and can't thus be virtual.
If you like, you could use other methods to carry out this function. For more detailed information, you could refer How to: override static methods: http://blogs.msdn.com/kirillosenkov/archive/2008/02/06/how-to-override-static-methods.aspx
Hope that helps.
Best Regards,
Nancy
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.- Marked As Answer by Nancy ShaoModerator Monday, February 09, 2009 3:47 AM

