Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Inheritance and 'virtual'

Posted on 2004-08-26
8
Medium Priority
?
322 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Windows Server 2003 introduced persistent Volume Shadow Copies and made 2003 a must-do upgrade.  Since then, it's been a must-implement feature for all servers doing any kind of file sharing.
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…
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…

877 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