DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Newbie question about function pointers/virtual functions.

  1. #1
    Join Date
    Aug 2005
    Location
    Slough
    Posts
    52

    Newbie question about function pointers/virtual functions.

    Hi,

    In an attempt to learn more C++ I'm converting some C code to C++. This C code is just one module with an associated structure and a load of functions that work on that structure so pretty easy to convert to a class.

    One thing though - currently you can register a logging function by passing a function pointer in to one of the functions. That function is stored away in the structure and called if defined. I presume I could leave that as it was but I'm wondering if I should use a feature of C++. I think a virtual function might be what I want but then, where I use this calss, I'd have to declare a class that inherits from this one just so that I can define the logging function. This seems a bit clumsy.

    Is there a better way or shall I leave it as it is?

    Appreciate any advice.

    Cheers

    Peter

  2. #2
    Join Date
    Nov 2003
    Posts
    4,118
    I don't see a prominent advantage in using a virtual function. Why not pass the address of the logging function as an optional parameter to the constructor? The constructor in turn will check whether the pointer has been provided (use a default value of NULL), and if so, it will call that function. Is this what you need or did I misunderstand your question?
    Danny Kalev

  3. #3
    Join Date
    Aug 2005
    Location
    Slough
    Posts
    52
    I think you've understood the problem correctly. I just wanted to make sure I'd not misunderstood virtual functions or inheretance. Thanks.

    One other thing while I'm here. I learnt most C++ from a Windows environment where the convention is to use m_ on member variables. I'm now learning more from general C++ books but they don't do that. Is there a generally accepted style for this sort of thing?

    Peter

  4. #4
    Join Date
    Nov 2003
    Posts
    4,118
    Quote Originally Posted by PeterS2
    I think you've understood the problem correctly. I just wanted to make sure I'd not misunderstood virtual functions or inheretance. Thanks.

    One other thing while I'm here. I learnt most C++ from a Windows environment where the convention is to use m_ on member variables. I'm now learning more from general C++ books but they don't do that. Is there a generally accepted style for this sort of thing?

    Peter
    Not at all:) That convention is a bit old fashioned. It dates back to the days when object orientation was all the rage and people were so excited about the very notion of wirting classes, data members and member functions that they decided they should laboriously document each construct in its name. This practice is still maintaines in MFC and Windows programming books but it's just a convention (and in my taste, not a particularly appetizing one). The convention I prefer is the one used in the C++ standard itself, i.e., lowercase letters and that's it:) The only exception I take is pointers whose names begin with an initial p, but still, there's nothing amiss with pointer variables that don't have an initial p (besides, state of the art C++ programs nowadays use less and less pointers anyway, replacing them with smart pointer objects, references and STL containers). It's good that you diversify your reading list. One of the advantages of this is that you're being exposed to diverse naming conventions and programming styles. Eventually, you'll develop one of your own.

    BTW, re. your original question: virtual functions make sense when you have different implementations of the same function. When you have a function that gets called optionally, there really is no point in using the heavy artillery of derived classes. Notice that derivation entails many other design decisions such as a virtual destructor, perhaps protected member functions and so on.
    Last edited by Danny; 08-28-2005 at 02:16 PM.
    Danny Kalev

  5. #5
    Join Date
    Aug 2005
    Location
    Slough
    Posts
    52
    Thanks for the info. :-)

Similar Threads

  1. comparing function pointers [was: typedef problem]
    By swapnil_paranja in forum C++
    Replies: 20
    Last Post: 08-25-2005, 03:54 PM
  2. Replies: 8
    Last Post: 04-02-2005, 11:54 AM
  3. Replies: 8
    Last Post: 03-08-2003, 07:25 PM
  4. NewBie question
    By Boosman in forum ASP.NET
    Replies: 1
    Last Post: 01-05-2002, 06:29 AM
  5. AnimateWindow API
    By Tim Manos in forum VB Classic
    Replies: 4
    Last Post: 10-19-2001, 06:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center