473,396 Members | 1,914 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,396 software developers and data experts.

How can i force my derivers to call a base function

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?
Dec 19 '07 #1
8 1673
On Dec 19, 12:41 pm, ManicQin <Manic...@gmail.comwrote:
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...
Dec 19 '07 #2
On Dec 19, 10:12 am, Rahul <sam_...@yahoo.co.inwrote:
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
Dec 19 '07 #3
On Dec 19, 10:28 am, ManicQin <Manic...@gmail.comwrote:
On Dec 19, 10:12 am, Rahul <sam_...@yahoo.co.inwrote:
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:ja*********@gmail.com
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
Dec 19 '07 #4
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
Dec 19 '07 #5


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.
Dec 19 '07 #6
On 2007-12-19 07:25:01 -0500, ManicQin <Ma******@gmail.comsaid:
>
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)

Dec 19 '07 #7
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 }
};
Dec 19 '07 #8
ManicQin <Ma******@gmail.comwrote in news:d4346905-60c3-4237-bb06-
3a**********@c4g2000hsg.googlegroups.com:
On Dec 19, 10:12 am, Rahul <sam_...@yahoo.co.inwrote:
> 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.
Dec 19 '07 #9

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

Similar topics

39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
3
by: Arran Pearce | last post by:
Hi, If i have a abstract class (e.g. Class1) and then i make Class2 which inherits from Class1. I have a method in Class1 which i want to force Class2 to run at some point. Is there a way i...
3
by: John A. Prejean | last post by:
This one has me stumped. I have a base form I am trying to wrap up, but I have one problem. In two functions I am opening a "record detail" form. I would like to keep the code in the base form...
5
by: rbfish | last post by:
Hi, How can I call a virtual base function with variable parameters? like, class base { public: int printf(const char *fmt, ...) { // do anything here.
6
by: Hubert Fritz | last post by:
Hello, I fear I want to have something which is not possible in C++. How is it possible to define a base class so that the derived class is forced to contain a static member variable, which...
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...
4
by: timor.super | last post by:
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...
6
Dormilich
by: Dormilich | last post by:
I've got 2 DB classes (see below) where one class extends my base DB class. I want the child classes (the User class is just an example) to have the static method getInstance() but I can't declare...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.