?
Solved

Inheritance and 'virtual'

Posted on 2004-08-26
8
Medium Priority
?
321 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 300 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
Microservice architecture adoption brings many advantages, but can add intricacy. Selecting the right orchestration tool is most important for business specific needs.
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…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

719 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