You are here:

C++/Since only one copy of each method is stored, what is the difference between static methods and normal methods?

Advertisement


Question
Hi,

Nice to see you again! my question is:

Since only one copy of each method is stored, what is the difference between static methods and normal methods?

Thanks,
lzzzz


Answer
Hello again.

I shall assume that by 'method' you mean 'member function' and shall ignore inline functions.

There is no real difference in the two except that instance member functions ('normal methods') arrange to have the class instance associated with each call. The usual way - and only way in use that I am aware of - is to provide a hidden function parameter for the this pointer. So under the hood the compiler generates code that passes the this pointer into the member function for instance member functions. As static member functions are not associated with specific instances of a class they have no this pointer and so do not need a hidden this pointer parameter.

So when you say something like:

       class C
       {
       public:
         void operation();
         void const_operation() const;
       };

The signature of the C::operation() member function generated by the compiler would conceptually look like:

       void C::operation( C * this );

and for C::const_operation() like so:

       void C::const_operation( C const * this );

Calling one of these member functions on a C instance thus:

       C c;
       c.operation();

is like calling a function called C::operation and passing in a pointer to the object thus:

       C c;
       C::operation( &c );

In fact this is similar to code that might be generated by a compiler that compiles C++ to C code which is then compiled using a C compiler to object code - although very simplified. One major difference would be in the naming of C::operation - some compiler specific name mangling scheme would be used to associate the class name, the function name, the member function qualifiers (const and volatile) and the function parameter types and maybe other stuff. For an example of the types of things a compiler considers when mangling names from your code see for example http://www.codesourcery.com/cxx-abi/abi.html, section 5.1 External Names (a.k.a Mangling).  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.