-
June 20th, 2013, 07:35 AM
#1
static function call
PHP Code:
class A { public: static void func(){std::cout<<"A\n";} };
class B:public A { public: virtual void func(){A::func();} };
class C:public B { public: virtual void funx(){func();} };
class D:public C { public: static void func(){std::cout<<"CC\n";} void Call(){funx();} };
int _tmain(int argc, _TCHAR* argv[]) { D b; b.Call(); return 0; }
I am lost in the basic *static* key applied for a function. Could someone please tell me why the above code print "A". I suppose it should be "CC".
-
June 20th, 2013, 07:57 AM
#2
Re: static function call
The compiler gives me this message
warning C4526: 'D::func' : static member function cannot override virtual function 'void B::func(void)
override ignored, virtual function will be hidden
-
June 20th, 2013, 08:01 AM
#3
Re: static function call
PHP Code:
class D:public C
{
public:
virtual void func(){std::cout<<"CC\n";}
void Call(){funx();}
};
and
PHP Code:
class D:public C
{
public:
void func(){std::cout<<"CC\n";}
void Call(){funx();}
};
From the OP example, could you tell me which one (please pay attention to the virtual key) is polymorphically implemented ? Does the virtual key above make its actual sense ? I don't think so.
-
June 20th, 2013, 08:04 AM
#4
Re: static function call
Originally Posted by GCDEF
The compiler gives me this message
warning C4526: 'D::func' : static member function cannot override virtual function 'void B::func(void)
override ignored, virtual function will be hidden
I don't have this warning on my VS2012 Ultimate... Please wait a second I look into compiler option....and be back very soon
(after 2 minutes I'm back and...)
I have level3 of warnings and see no warning(C4526), even after I enable all warnings(/Wall)
Last edited by terminalXXX; June 20th, 2013 at 08:08 AM.
-
June 20th, 2013, 08:23 AM
#5
Re: static function call
Originally Posted by terminalXXX
I don't have this warning on my VS2012 Ultimate... Please wait a second I look into compiler option....and be back very soon
(after 2 minutes I'm back and...)
I have level3 of warnings and see no warning(C4526), even after I enable all warnings(/Wall)
I get the same warning as GCDEF in post #2 with level 3.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 20th, 2013, 08:28 AM
#6
Re: static function call
Ok thank you you two a lot, what about my third post question ?
(Oppps, after 1 minute I posted this, I realize I was wrong because ...)
virtual key in the base class is meant for polymophism, but in the child isn't supposed to be so unless it does have any children then, right ? So this is simply a chain of function calls, nothing magical about this between virtual and non-virtual. It is not polymorphism at all.
Last edited by terminalXXX; June 20th, 2013 at 08:33 AM.
-
June 20th, 2013, 08:29 AM
#7
Re: static function call
Originally Posted by terminalXXX
I don't have this warning on my VS2012 Ultimate... Please wait a second I look into compiler option....and be back very soon
(after 2 minutes I'm back and...)
I have level3 of warnings and see no warning(C4526), even after I enable all warnings(/Wall)
Regardless, that's the problem. You can search that warning for more information.
-
June 20th, 2013, 08:31 AM
#8
Re: static function call
From the OP example, could you tell me which one (please pay attention to the virtual key) is polymorphically implemented ? Does the virtual key above make its actual sense ? I don't think so.
To implement polymorphically, the function in the base class needs to be declared virtual. Derived classes from the base class can declare the overridden functions as virtual but it is not needed once the base class function has been declared virtual. Static class functions are functions that can called outside of an instance (eg from your code A::func()). These class static functions do not have access to the 'this' pointer and so cannot access class variables etc.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
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
|