LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2011, 10:21 PM   #16
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940

Thanks for the effort, John.
Quote:
Originally Posted by johnsfine View Post
The compilation error meant exactly what it said. The parameter you used for DerivedMethod (std::string) did not match any parameter you declared for DerivedMethod.
How daft of me :doh: That's all I have to say!

Quote:
Originally Posted by johnsfine View Post
I'm sure you're used to the fact that you can normally pass a value when a & is expected and you can normally pass a non const when a const is expected. So it is confusing that in this case you can't.
I need to study more on const std :: string&.

Quote:
Originally Posted by johnsfine View Post
I'm used to getting a compiler warning for that mistake. But I haven't tested to see why you don't get it in this case.
I tested the below program once again, with Wall and Wextra, it doesn't give me any sorts of
warnings!
Code:
#include <iostream>
#include <vector>
using namespace std;

template <class MyDummyClass, typename ReturnType, typename Parameter>
class SingularCallBack
{
public:
	  typedef ReturnType (MyDummyClass ::*Method)(Parameter);
	  
	  SingularCallBack(MyDummyClass* _class_instance, Method _method)
	  {
	       class_instance = _class_instance;
	       method             = _method;
	  };

	  ReturnType execute(Parameter parameter)
	  {
	       return (class_instance->*method)(parameter);
	  };

private:
	  MyDummyClass*   class_instance;
	  Method  method;
};


class BaseClass
{
   public:
     virtual bool DerivedMethod (const std::string str)
     {
	  cout << str;
	  return true; 
     }
};

class AClass : public BaseClass
{
public:
  AClass(unsigned int _id): id(_id){}

  bool DerivedMethod( const std::string& str)
  {
     std::cout << "Derived Method AClass[" << id << "]: " << str << std::endl;
     return true;
  }

private:
 unsigned int id;
};

int main()
{
   std::vector < SingularCallBack < BaseClass, bool, std::string > > callback_list;

   AClass a(1);

   callback_list.push_back (SingularCallBack < BaseClass, bool, std::string > (&a, &BaseClass :: DerivedMethod));
   
   for (unsigned int i = 0; i < callback_list.size(); ++i)
   {
      callback_list[i].execute("abc");
   }

   return 0;
}
Quote:
Originally Posted by johnsfine View Post
The derived class's function has a different signature than the base class's virtual function, so the derived function masks the base function rather than overriding it.
I never knew that we are allowed to change the signatures of the virtual functions in the derived classes.

Quote:
Originally Posted by johnsfine View Post
A related tricky topic is buried in the meaning of
BaseClass::DerivedMethod when used in the context of a derived object...That strange difference between BaseClass::DerivedMethod and &BaseClass::DerivedMethod has confused a lot of C++ programmers.
I am really thankful to you for all the effort you put in that explanation, but the situation is still not crystal clear for me. I request you to tell me what keywords should
I put in Google to get more on the difference between BaseClass::DerivedMethod and &BaseClass::DerivedMethod.

I have the thinking in C++ book where the VTABLES are explained in detail, are
the VTABLES which I need to study in order to understand the above differences?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-07-2011, 11:40 PM   #17
Aquarius_Girl
Senior Member
 
Registered: Dec 2008
Posts: 4,731

Original Poster
Blog Entries: 29

Rep: Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940Reputation: 940
Quote:
Originally Posted by johnsfine View Post
I'm used to getting a compiler warning for that mistake. But I haven't tested to see why you don't get it in this case. The derived class's function has a different signature than the base class's virtual function, so the derived function masks the base function rather than overriding it.
Now I realize, when we change the signature of the base class virtual function in the derived class, it becomes a new function, and then calling it with Baseclass :: Derived will not call the derived classes virtual function since there is none and that's the reason compiler didn't give any warnings.

Last edited by Aquarius_Girl; 03-07-2011 at 11:43 PM.
 
  


Reply

Tags
c++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regarding sockets and callbacks manohar Programming 1 12-21-2010 04:40 PM
gtk problem with callbacks mvan83 Programming 3 12-13-2005 06:07 PM
Callbacks in Squid s_araj Programming 2 07-02-2005 06:06 AM
callbacks in C? MylesCLin Programming 2 02-10-2005 10:27 AM
include for callbacks.c abs Programming 5 04-07-2004 02:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration