Solved

Inheritance and 'virtual'

Posted on 2004-08-26
8
317 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

734 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