473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to force overriding

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.

May 1 '07 #1
4 2325
ti*********@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 1 '07 #2
ti*********@gmail.com wrote:
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().

May 1 '07 #3
On 1 mai, 16:05, Rolf Magnus <ramag...@t-online.dewrote:
timor.su...@gmail.com wrote:
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().
Thanks for your help
this is exactly what i want

Best regards,

S.

May 1 '07 #4
"Victor Bazarov" <v.********@comAcast.netwrote in news:f17hi7$ios$1
@news.datemas.de:
ti*********@gmail.com wrote:
>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;
}
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;
};
May 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: NotGiven | last post by:
I am sending email using the mail() function on a hosted account. I am trying to override the RETURN-PATH variable but it is not working. The server keeps overriding the variable I put in. ...
3
by: Andrew Durdin | last post by:
In Python, you can override the behaviour of most operators for a class, by defining __add__, __gt__, and the other special object methods. I noticed that, although there are special methods for...
3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
9
by: James Marshall | last post by:
I'm writing a library where I want to override document.write(), but for all document objects; thus, I want to put it in the prototype. I tried Document.prototype.write= my_doc_write ; but it...
4
by: Asipu | last post by:
The problem is quite difficult and i'm starting to think that it's impossible to accomplish.. Suppose you have C.exe. When you run C.exe, it must change his 0x4e's byte into 0xff.. Of course,...
3
by: Amin Sobati | last post by:
Hi, I have two classes. Class2 inhertis Class1: ----------------------------- Public Class Class1 Public Overridable Sub MySub() End Sub End Class Public Class Class2
1
by: Bob | last post by:
I'm asking here because I've posted this question in the crystal newsgroup, waited two weeks, and got zero response. I would be very appreciative if someone could shed some light on this problem...
6
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.