Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How can i force my derivers to call a base function

Reply
Thread Tools

How can i force my derivers to call a base function

 
 
ManicQin
Guest
Posts: n/a
 
      12-19-2007
Hi lets say I have the next situation
class base
{
pubic:
base() {}
virtual ~base(){}
virtual init()
{//do init }
};

now i want that every deriver will call MY init function before he
init his class...
for some reasons I cannot put those calls in the cTor...

one thing that I thought of is
class base
{
public:
init()
{
//do base's init
secondPhaseInit();
}
protected:
virtual secondPahseInit() = 0;
}

and then the deriver can only override secondPhaseInit and he needs to
go through my init .
But it's still not an idiot proof method... is there a way?
 
Reply With Quote
 
 
 
 
Rahul
Guest
Posts: n/a
 
      12-19-2007
On Dec 19, 12:41 pm, ManicQin <(E-Mail Removed)> wrote:
> Hi lets say I have the next situation
> class base
> {
> pubic:
> base() {}
> virtual ~base(){}
> virtual init()
> {//do init }
>
> };
>
> now i want that every deriver will call MY init function before he
> init his class...
> for some reasons I cannot put those calls in the cTor...
>

Could you specify the details of the reason?
Derived class constructor can invoke any of the visible base class
member functions, which is one of the reason for the base class
constructor to be invoked before that of derived class constructor...
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      12-19-2007
* ManicQin:
> Hi lets say I have the next situation
> class base
> {
> pubic:
> base() {}
> virtual ~base(){}
> virtual init()
> {//do init }
> };
>
> now i want that every deriver will call MY init function before he
> init his class...
> for some reasons I cannot put those calls in the cTor...
>
> one thing that I thought of is
> class base
> {
> public:
> init()
> {
> //do base's init
> secondPhaseInit();
> }
> protected:
> virtual secondPahseInit() = 0;
> }
>
> and then the deriver can only override secondPhaseInit and he needs to
> go through my init .
> But it's still not an idiot proof method... is there a way?


Your question is extremely unclear.

If it's just about how to achieve two-phase construction, the best is
probably to read up on class invariants in order to understand why that
is a generally a Bad Idea.

Otherwise, have a look at the FAQ, e.g. <url:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.6>.


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
ManicQin
Guest
Posts: n/a
 
      12-19-2007
On Dec 19, 10:12 am, Rahul <(E-Mail Removed)> wrote:

> Could you specify the details of the reason?
> Derived class constructor can invoke any of the visible base class
> member functions, which is one of the reason for the base class
> constructor to be invoked before that of derived class constructor...


Thank you rahul on that great introduction to c++ inheritence.
And thank you Alf.

I probably misled you both because i called my function init.
The question is not about cTor or initialization in particularly.

take the next snip

class base
{
public:

foo()
{ /do something - any thing!
goo();
}
protected:
virtual goo() = 0;
}

class derive : public base
{
protected:
goo()
{
//do something else...
}

}

int main()
{
derive a;

a.foo(); //first base.foo() will execute and then derive.goo()
return 0;
}

the problem is that this pattern (I think they call it Template
Method) is not IDIOT PROOF.
tomorrow a coder would come and change my code so he would'nt call my
base function. (infact it happened today in spite of all the comments
i left!) I'm just looking for a different way to implement this.

thank you
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      12-19-2007
On Dec 19, 10:28 am, ManicQin <(E-Mail Removed)> wrote:
> On Dec 19, 10:12 am, Rahul <(E-Mail Removed)> wrote:


> I probably misled you both because i called my function init.
> The question is not about cTor or initialization in
> particularly.


> class base
> {
> public:
> foo()
> { /do something - any thing!
> goo();
> }
> protected:


Why protected, and not private?

> virtual goo() = 0;
> }


> class derive : public base
> {
> protected:
> goo()
> {
> //do something else...
> }
> }


> int main()
> {
> derive a;


> a.foo(); //first base.foo() will execute and then derive.goo()
> return 0;
> }


> the problem is that this pattern (I think they call it Template
> Method) is not IDIOT PROOF.
> tomorrow a coder would come and change my code so he would'nt
> call my base function. (infact it happened today in spite of
> all the comments i left!) I'm just looking for a different way
> to implement this.


I'm not sure I understand. Which code are you talking about?
If client code doesn't call some particular function, there's
not much you can do about it. For that matter, he may not even
create an instance of your class. And the author of the derived
class shouldn't call your base class function; that would result
in endless recursion. Could you please give an exact example of
the type of error you're trying to avoid.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Stuart Redmann
Guest
Posts: n/a
 
      12-19-2007
ManicQin wrote:
> class base
> {
> public:
>
> foo()
> { /do something - any thing!
> goo();
> }
> protected:
> virtual goo() = 0;
> }
>
> class derive : public base
> {
> protected:
> goo()
> {
> //do something else...
> }
>
> }
>
> int main()
> {
> derive a;
>
> a.foo(); //first base.foo() will execute and then derive.goo()
> return 0;
> }
>
> the problem is that this pattern (I think they call it Template
> Method) is not IDIOT PROOF.
> tomorrow a coder would come and change my code so he would'nt call my
> base function. (infact it happened today in spite of all the comments
> i left!) I'm just looking for a different way to implement this.


This is still too little information for us, we cannot assess whether the
overall design is flawed. Why is it necessary that the user calls your base
class method? Maybe your class derive should not even be derived from base if
you have such strange dependencies in your code.

Regards,
Stuart
 
Reply With Quote
 
ManicQin
Guest
Posts: n/a
 
      12-19-2007


James Kanze:
> Why protected, and not private?

That was a typo, sorry.


Stuart Redmann :
> This is still too little information for us, we cannot assess whether the
> overall design is flawed. Why is it necessary that the user calls your base
> class method? Maybe your class derive should not even be derived from base if
> you have such strange dependencies in your code.


My question was'nt about the design.
I think that if after 4 threads I didnt manage to explain my question,
then maybe it is better to just leave it be.

thank you for your help.
 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      12-19-2007
On 2007-12-19 07:25:01 -0500, ManicQin <(E-Mail Removed)> said:

>
> My question was'nt about the design.
>


The question wasn't, but the answer may well be. Awkward coding
problems often can be resolved by changing the design. Especially when
the immediate answer to the awkward coding problem is, as here, "you
can't do that."

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      12-19-2007
ManicQin wrote:
> Hi lets say I have the next situation
> class base
> {
> pubic:

Yeah, I know it's a typo, but you have to admit, this is a *GREAT* way
to define your private parts
> base() {}
> virtual ~base(){}
> virtual init()
> {//do init }
> };
>

 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      12-19-2007
ManicQin <(E-Mail Removed)> wrote in news:d4346905-60c3-4237-bb06-
http://www.velocityreviews.com/forums/(E-Mail Removed):

> On Dec 19, 10:12 am, Rahul <(E-Mail Removed)> wrote:
>
>> Could you specify the details of the reason?
>> Derived class constructor can invoke any of the visible base class
>> member functions, which is one of the reason for the base class
>> constructor to be invoked before that of derived class constructor...

>
> Thank you rahul on that great introduction to c++ inheritence.
> And thank you Alf.
>
> I probably misled you both because i called my function init.
> The question is not about cTor or initialization in particularly.
>
> take the next snip
>
> class base
> {
> public:
>
> foo()
> { /do something - any thing!
> goo();
> }
> protected:
> virtual goo() = 0;
> }
>
> class derive : public base
> {
> protected:
> goo()
> {
> //do something else...
> }
>
> }
>
> int main()
> {
> derive a;
>
> a.foo(); //first base.foo() will execute and then derive.goo()
> return 0;
> }
>
> the problem is that this pattern (I think they call it Template
> Method) is not IDIOT PROOF.
> tomorrow a coder would come and change my code so he would'nt call my
> base function. (infact it happened today in spite of all the comments
> i left!) I'm just looking for a different way to implement this.


You cannot prevent malice. Severly discipline the other developer for
breaking the design without consulting the appropriate stakeholders.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Nike air force one, air force 1, air force one low cut, air force one abdul_razak@indiatimes.com Digital Photography 2 12-31-2008 04:29 PM
Nike Air Force Ones,Air Force One Air Force One-1 lky52193@gmail.com Computer Support 0 01-17-2008 04:40 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:46 PM
Nike Air Force Ones,Air Force One Air Force One-1,25th anniversary lky52112@gmail.com Digital Photography 0 01-15-2008 04:34 PM



Advertisments