Solved

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

Posted on 2010-11-09
9
521 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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 125 total points
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 32

Expert Comment

by:phoffric
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
One big step towards C++ mastery :)
Good luck learning!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now