how to force overriding

T

timor.super

Hi,
I would like to make a class, that if another class derives from this,
people has to override somes functions, obligatory.

I can do that with pure virtual function, like this :

class Test
{
public:
Test()
{
cout << "Ctor Test" << endl;
}
virtual void fToOverride() = 0;
};

but, what i would like to do, is to force to override, but that my
function do something :

class Test
{
public:
Test()
{
cout << "Ctor Test" << endl;
}
virtual void fToOverride()
{
cout << "i'm here" << endl;
}
};

I would like that people that derives from Test, have to override the
function fToOverride and when calling it, that it does output "i'm
here" and do what they have written in their derived class.

How to do that ? (i hope i've been clear enough)

Thanks for your help,

S.
 
R

Rolf Magnus

Hi,
I would like to make a class, that if another class derives from this,
people has to override somes functions, obligatory.

I can do that with pure virtual function, like this :

class Test
{
public:
Test()
{
cout << "Ctor Test" << endl;
}
virtual void fToOverride() = 0;
};

but, what i would like to do, is to force to override, but that my
function do something :

class Test
{
public:
Test()
{
cout << "Ctor Test" << endl;
}
virtual void fToOverride()
{
cout << "i'm here" << endl;
}
};

I would like that people that derives from Test, have to override the
function fToOverride and when calling it, that it does output "i'm
here" and do what they have written in their derived class.

How to do that ? (i hope i've been clear enough)

You can make it pure virtual and implement it, but it will only be called if
the derived class's implementation explicitly calls it. Otherwise you can
wrap the function, like:

class Test
{
public:
void fToOverride()
{
cout << "i'm here" << endl;
do_fToOverride();
}
private:
virtual void do_fToOverride() = 0;
};

Now the derived class must override do_fToOverride().
 
V

Victor Bazarov

I would like to make a class, that if another class derives from this,
people has to override somes functions, obligatory.

I can do that with pure virtual function, like this :

class Test
{
public:
Test()
{
cout << "Ctor Test" << endl;
}
virtual void fToOverride() = 0;
};

but, what i would like to do, is to force to override, but that my
function do something :

class Test
{
public:
Test()
{
cout << "Ctor Test" << endl;
}
virtual void fToOverride()
{
cout << "i'm here" << endl;
}
};

I would like that people that derives from Test, have to override the
function fToOverride and when calling it, that it does output "i'm
here" and do what they have written in their derived class.

How to do that ? (i hope i've been clear enough)

Declare it pure in the class definition and define it (provide it with
a body) *outside* of the class:

class HasPureImplemented {
public:
void fToOverride() = 0;
};

void HasPureImplemented::fToOverrid()
{
cout << "i'm here" << endl;
}

V
 
T

timor.super

You can make it pure virtual and implement it, but it will only be called if
the derived class's implementation explicitly calls it. Otherwise you can
wrap the function, like:

class Test
{
public:
void fToOverride()
{
cout << "i'm here" << endl;
do_fToOverride();
}
private:
virtual void do_fToOverride() = 0;

};

Now the derived class must override do_fToOverride().

Thanks for your help
this is exactly what i want

Best regards,

S.
 
A

Andre Kostur

Declare it pure in the class definition and define it (provide it with
a body) *outside* of the class:

class HasPureImplemented {
public:
void fToOverride() = 0;
};

void HasPureImplemented::fToOverrid()
{
cout << "i'm here" << endl;
}

Not quite what the OP is asking for. You'd have to rely on the subclass
implementor to chain back to your implementation.

What you could do is have the base class with a non-virtual function
fToOverride(), and it calls a different pure virtual function to allow
the subclass implementator to do their thing:

class Base
{
public:
void fToOverride()
{
cout << "I'm here" << endl;
this->f_ToOverride_i();
}

private:
virtual void fToOverride_i() = 0;
};
 

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
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top