Solved

Inheritance and 'virtual'

Posted on 2004-08-26
8
320 Views
Last Modified: 2013-11-15
class Map
{
   void Put(const char*, const char*);
   // ...
}

class Settings : public Map
{
   Put(const char*, const char*);
   // ...
}


Obviously this code is wrong as Map::Put() is not virtual. What I am wanting is for Settings to be able to do a small bit of processing *before* passing the parameters up to Map::Put(), but don't want the implementation of Map::Put() to be completely overridable. I don't want to have to change the inheritance to private.

Is this acheivable?
--Rob
0
Comment
Question by:boycy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 11900839
class Map
{
   virtual void Put(const char*, const char*);
   // ...
}

class Settings : public Map
{
   Put(const char*, const char*);
   // ...
}


Settings::Put (const char* pc1, const char* pc2)
{
  //do your processing here
  Map::Put (pc1, pc2);
}
0
 
LVL 6

Author Comment

by:boycy
ID: 11900949
Quote "don't want the implementation of Map::Put() to be completely overridable".

--Rob
0
 
LVL 13

Accepted Solution

by:
SteH earned 100 total points
ID: 11900974
Can you then specify what you exactly want to do. You can make the function non virtual and do the same.
class Map
{
   void Put(const char*, const char*);
   // ...
}

class Settings : public Map
{
   void Put(const char*, const char*);
   // ...
}


Settings::Put (const char* pc1, const char* pc2)
{
  //do your processing here before calling
  Map::Put (pc1, pc2);
}

or do you need only parts of the functionality of Map::Put implemented? In that case you could seperate that part into another function of Map and call this function from Map::Put and Settings::Put.
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 6

Author Comment

by:boycy
ID: 11901016
Overriding a non-virtual function will compile but the behaviour is undefined.
I want all of Map::Put() to be run as part of Settings::Put() after some extra checking.

Basically, the second char* for Map::Put() may be 0 and I need it to stay that way, and Map::Put() should not be completely re-implementable therefore shouldn't be virtual.
Settings::Put() needs to impose a restriction that the second char* may *not* be 0 before passing the parameters on to Map::Put().

Cheers,
Rob
0
 
LVL 13

Expert Comment

by:SteH
ID: 11901067
Why is the behaviour undefined. A virtual function will do a check at runtime which version to call. A non virtual function is linked at compile time. So you can only call Settings::Put using a pointer to or and object of class Settings. You need to check that you have a proper object for calling it. Both of these functions are defined in different namespaces (the classes) so what is undefined?
0
 
LVL 6

Author Comment

by:boycy
ID: 11901155
OK, I take it back - I'd read a couple of hours ago that overriding non-virtuals causes undefined behaviour, but just read in another place that it's legal (but immoral!).

Settings::Put(const char*psz1, const char *psz2)
{
   if (psz2 == 0)
      throw ...;
   Map::Put(psz1, psz2);
}

Assuming that Map::Put is a non-virtual function, is this completely legal valid code which won't produce any nasty behaviour or do anything unexpected?

Rob
0
 
LVL 2

Expert Comment

by:carribus
ID: 11901209
virtual functions are provided for polymorphism, i.e. using a base class pointer to call overridden functions in derived classes... so in your case:

Map*   pMapObj = new Settings;

pMap->Put();

if your Put function was virtual in Map, then in the above code, the Settings::Put() function would be called. However, if the Map function is NOT virtual, the Map::Put() function will be called.

SteH is correct. You literally override the Map::Put() function in the Settings class and then call via scoping into the Map::Put() function.
0
 
LVL 6

Author Comment

by:boycy
ID: 11901330
OK, cheers both.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

How to fix error ""Failed to validate the vCentre certificate. Either install or verify the certificate by using the vSphere Data Protection Configuration utility" when you are trying to connect to VDP instance from Vcenter.
How to update Firmware and Bios in Dell Equalogic PS6000 Arrays and Hard Disks firmware update.
This tutorial will walk an individual through the process of installing of Data Protection Manager on a server running Windows Server 2012 R2, including the prerequisites. Microsoft .Net 3.5 is required. To install this feature, go to Server Manager…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

617 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