How can i force my derivers to call a base function

M

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?
 
R

Rahul

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...
 
A

Alf P. Steinbach

* 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
 
M

ManicQin

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
 
J

James Kanze

On Dec 19, 10:12 am, Rahul <[email protected]> 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.
 
S

Stuart Redmann

ManicQin said:
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
 
M

ManicQin

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.
 
P

Pete Becker

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."
 
R

red floyd

ManicQin said:
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 :)
 
A

Andre Kostur

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.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top