Solved

Inheritance and 'virtual'

Posted on 2004-08-26
8
301 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
  • 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
 
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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

VM backups can be lost due to a number of reasons: accidental backup deletion, backup file corruption, disk failure, lost or stolen hardware, malicious attack, or due to some other undesired and unpredicted event. Thus, having more than one copy of …
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 the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

758 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

22 Experts available now in Live!

Get 1:1 Help Now