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

Posted on 2010-11-09
Medium Priority
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 500 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?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 32

Assisted Solution

phoffric earned 500 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 500 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 500 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

Industry Leaders: 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!

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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