what is the meaning of const at the end of the class member function

Posted on 2010-11-09
Last Modified: 2012-05-10
hi all,

I am not sure whether my title describe the problem correctly or not. Apologise if i have not.

I have a class called CustLoaderFactory which is derived from TargetFactory and ObjectCollector class.

class CustLoaderFactory : public TargetFactory<TsfLoader>, ,public ObjectCollector<TsfLoader>

by the way, may i know what is the purpose of <TsfLoader> in above code?

then in both TargetFactory and ObjectCollector class, it declared the member function with a const at the end.

virtual int func1(char *a, const void *b) = 0;
what is the purpose of the const 0? And how do i declare it in my body program?

header file
class a : public b<TsfLoader>, ,public c<TsfLoader>
virtual int func 1(char *1a, const void *1b) = 0;

body code
int a::func1(char *1a, const void *1b)
    strcpy(1a, (char *)1b);
    return 0;

So, how do i call the func1 from my body code since it has a const at the back.

Beside that, since class a derive from class b and class c. by writing my own definition in body code for func1, am i  overiding the original definition?

I am testing out some sample code to get more understanding on the c++ OOP.
Thank you in advance.
Question by:gagajanice
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
LVL 40

Accepted Solution

evilrix earned 125 total points
ID: 34098735
This explains all about const member functions.
LVL 40

Expert Comment

ID: 34098741
>> by the way, may i know what is the purpose of  in above code?

It's a template class so TsfLoader is the template parameter

Author Comment

ID: 34099099

the const that i have after the class member function is not a suffix const but a const integer:

virtual int func1(char *a, const void *b) = 0; (equal to integer "0").

Are they the same?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 32

Assisted Solution

phoffric earned 125 total points
ID: 34099109
The 0 is a special symbol in this context meaning that the function, func1, is not only a virtual function, but is is also a pure virtual function.

"In C++, pure virtual functions are declared using a special syntax [ = 0 ] as demonstrated below."

class Abstract {
   virtual void pure_virtual() = 0;

LVL 32

Expert Comment

ID: 34099119
Inclusion of a pure virtual function in a class makes that class abstract. FYI - an abstract class cannot be instantiated.

Assisted Solution

by:Subrat (C++ windows/Linux)
Subrat (C++ windows/Linux) earned 125 total points
ID: 34099781
In your code no where I found 'const' at the end of the class member function.

Something regarding const....
I'm giving you one ex:

class A {
     int a;
      void SetData(int x){
          a = x;

      int  GetData( ) const {
          // a = 20; -------------------------------------------> Gives error
            retun a;

If using const after member function as shown above, then that function can't modify any data member of that class. Here in reality we don't want GetData() to modify the data members, So making it const.


Assisted Solution

Xper4net earned 125 total points
ID: 34101625
Adding to Subrat2009 comment: in a const method like GetData, you are also restricted by calling only others const methods.
For example:
int GetData const {
// SetData(20) ------------> Gives error too !

But in C++, with most compilers, there's a workaround :
int GetData const{
((A*)this)->SetData(20) ; // yes, it works ! But it's not a good practice...

Author Closing Comment

ID: 34108380
hi all,

sorry for getting back to you all on this a bit late. this is because i am going through the link provided by you all yesterday.

actually, i was asking what is the purpose of having '= 0' at the back of the member function.

thanks phoffric for telling me is a special syntax of pure virtual function.

I somehow manage to compile my program with the guidance from you all.

Thank you very much. Have a nice day.
LVL 32

Expert Comment

ID: 34108390
One big step towards C++ mastery :)
Good luck learning!

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question