• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 580
  • Last Modified:

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

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
gagajanice
Asked:
gagajanice
  • 3
  • 2
  • 2
  • +2
4 Solutions
 
evilrixSenior Software Engineer (Avast)Commented:
This explains all about const member functions.
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.10
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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
 
gagajaniceAuthor Commented:
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
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!

 
phoffricCommented:
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
 
phoffricCommented:
Inclusion of a pure virtual function in a class makes that class abstract. FYI - an abstract class cannot be instantiated.
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
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
 
Xper4netCommented:
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
 
gagajaniceAuthor Commented:
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
 
phoffricCommented:
One big step towards C++ mastery :)
Good luck learning!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now