Multi Inheritance

A

Ali Karaali

Hello group !

I have a class interface (as .h ) but i don't have this class's source
file (as .cpp ). So i can't reach the source file..

class ServerBase {
public :
virtual void vfunc() const;
void nvfunc() const;
~ServerBase();
};

class ServerDer : public ServerBase {
public :
void vfunc() const;
~ServerDer();
};

I want to make nvfunc() and ~ServerBase functions virtual. how can i
do that?
 
A

Alf P. Steinbach

* Ali Karaali:
Hello group !

I have a class interface (as .h ) but i don't have this class's source
file (as .cpp ). So i can't reach the source file..

class ServerBase {
public :
virtual void vfunc() const;
void nvfunc() const;
~ServerBase();
};

class ServerDer : public ServerBase {
public :
void vfunc() const;
~ServerDer();
};

I want to make nvfunc() and ~ServerBase functions virtual. how can i
do that?

You can't. That is, you can't make the existing code call your nvfunc()
implementation. But you can obtain that effect for your own code, by
wrapping the class, e.g.

class MyServerBase
{
private:
virtual ServerBase& impl() = 0;
public:
virtual ~MyServerBase() {}
virtual void nvfunc() const { impl().nvfunc(); }
};

class MyServerDer: public MyServeBase
{
private:
ServerDer myImpl;
ServerDer& impl() { return myImpl; }
public:
virtual void nvfunc() cosnt { ... }
...
};

But the question is why would you want to do that.

Presumably what nvfunc does is to wrap vfunc with some precondition and
postcondition checking, not to be overridden by you.

It's could be bad design not to have the ServerBase destructor virtual,
yes, but presumably these classes are designed to be used via smart
pointers that ensure destruction via pointer to most derived.
 
A

Ali Karaali

You can't. That is, you can't make the existing code call your nvfunc()
implementation. But you can obtain that effect for your own code, by
wrapping the class, e.g.

class MyServerBase
{
private:
virtual ServerBase& impl() = 0;
public:
virtual ~MyServerBase() {}
virtual void nvfunc() const { impl().nvfunc(); }
};

class MyServerDer: public MyServeBase
{
private:
ServerDer myImpl;
ServerDer& impl() { return myImpl; }
public:
virtual void nvfunc() cosnt { ... }
...
};

Maybe i think it can be, like this

class NewBase {
public :
virtual void vfunc() const = 0;
virtual void nvfunc() const = 0;
virtual void new_vfunc() const = 0;
virtual ~NewBase()
{
std::cout<<"NewBase::~NewBase()\n";
}
};

class Join : public NewBase , public ServerBase {
public :
void vfunc() const
{ std::cout<<"Join::vfunc() const\n"; }
void nvfunc() const
{ std::cout<<"Join::nvfunc() const\n"; }
void new_vfunc() const
{ std::cout<<"Join::new_vfunc const\n"; }
~Join()
{ std::cout<<"Join::~Join()\n"; }
};

L ke that but i don't understand explicitly? How can do that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top