Solved

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

Posted on 2010-11-09
9
541 Views
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.

eg
---
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.

eg.
---
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?

eg.
header file
-------------
class a : public b<TsfLoader>, ,public c<TsfLoader>
{
public:
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.
0
Comment
Question by:gagajanice
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 125 total points
ID: 34098735
This explains all about const member functions.
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10
0
 
LVL 40

Expert Comment

by:evilrix
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
http://www.parashift.com/c++-faq-lite/templates.html
0
 

Author Comment

by:gagajanice
ID: 34099099
hi,

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?
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 32

Assisted Solution

by:phoffric
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.
   http://en.wikipedia.org/wiki/Virtual_function#Abstract_classes_and_pure_virtual_functions

"In C++, pure virtual functions are declared using a special syntax [ = 0 ] as demonstrated below."
    http://en.wikipedia.org/wiki/Virtual_function#C.2B.2B_2

class Abstract {
public:
   virtual void pure_virtual() = 0;
};

0
 
LVL 32

Expert Comment

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

Assisted Solution

by:Subrat (C++ windows/Linux)
Subrat (C++ windows/Linux) earned 125 total points
ID: 34099781
Hi,
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 {
  private:
     int a;
   public:
      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.

0
 
LVL 5

Assisted Solution

by:Xper4net
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...
0
 

Author Closing Comment

by:gagajanice
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.
0
 
LVL 32

Expert Comment

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 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