By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,640 Members | 2,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,640 IT Pros & Developers. It's quick & easy.

How to block member overridable

P: n/a
Hi,

I have following classes:

class A
{
public:
virtual bool foo() = 0;
};

class B : public A
{
protected:
int state;

public:
int GetState() { return state; }

virtual bool foo()
{
// implementation which sets B::state to same value
}
}

Method B::foo() sets B::state variabl to some value. This variable may be
further used by other parts of code so its value is very important to be
vaild. The problem is that someone may construct class C like follows:

class C : public B
{
public:
virtual bool foo()
{
// implementation which does not set variable 'state'
}
}

In effect B::state has invalid value and some other parts of code which uses
B::state may crash. How do i block function foo so class C will not be able
to override it ? Is it possible in C++ ? Or is some other workaround to
problem i mentioned ?

--
With regards,
Marcin Prochownik
Jun 21 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ajk
On Thu, 21 Jun 2007 11:41:41 +0200, "Marcin Prochownik"
<ma*******@tlen.plwrote:
>class B : public A
{
protected:
int state;

public:
int GetState() { return state; }

virtual bool foo()
{
// implementation which sets B::state to same value
}
}
why do you declare foo virtual in B if you don't want it overridden?
Jun 21 '07 #2

P: n/a
On Jun 21, 6:41 pm, "Marcin Prochownik" <manneq...@tlen.plwrote:
Hi,

I have following classes:

class A
{
public:
virtual bool foo() = 0;

};

class B : public A
{
protected:
int state;

public:
int GetState() { return state; }

virtual bool foo()
{
// implementation which sets B::state to same value
}

}

Method B::foo() sets B::state variabl to some value. This variable may be
further used by other parts of code so its value is very important to be
vaild. The problem is that someone may construct class C like follows:

class C : public B
{
public:
virtual bool foo()
{
// implementation which does not set variable 'state'
}

}

In effect B::state has invalid value and some other parts of code which uses
B::state may crash. How do i block function foo so class C will not be able
to override it ? Is it possible in C++ ? Or is some other workaround to
problem i mentioned ?

--
With regards,
Marcin Prochownik
Are you trying to block the overriding or name hiding?

Jun 21 '07 #3

P: n/a
On 21 Jun, 10:41, "Marcin Prochownik" <manneq...@tlen.plwrote:
>
In effect B::state has invalid value and some other parts of code which uses
B::state may crash. How do i block function foo so class C will not be able
to override it ? Is it possible in C++ ? Or is some other workaround to
problem i mentioned ?
redesign your code so that state member is initialized
in the constructor. that is what constructors are
for anyway, to initialize the state of an object to
some meaningful state.

regards

DS

Jun 21 '07 #4

P: n/a
On 2007-06-21 11:41, Marcin Prochownik wrote:
Hi,

I have following classes:

class A
{
public:
virtual bool foo() = 0;
};

class B : public A
{
protected:
int state;

public:
int GetState() { return state; }

virtual bool foo()
{
// implementation which sets B::state to same value
}
}

Method B::foo() sets B::state variabl to some value. This variable may be
further used by other parts of code so its value is very important to be
vaild. The problem is that someone may construct class C like follows:

class C : public B
{
public:
virtual bool foo()
{
// implementation which does not set variable 'state'
}
}

In effect B::state has invalid value and some other parts of code which uses
B::state may crash. How do i block function foo so class C will not be able
to override it ? Is it possible in C++ ? Or is some other workaround to
problem i mentioned ?
Add a comment (either in the code or in the documentation) that if
someone overrides the method they must call B::foo(). It works wonder in
a number of frameworks I've used. After all, if an object of type C does
not work correctly unless they call B::foo() then I'm pretty sure
they'll want to do that.

--
Erik Wikström
Jun 21 '07 #5

P: n/a
On Jun 21, 10:14 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-06-21 11:41, Marcin Prochownik wrote:
Hi,
I have following classes:
class A
{
public:
virtual bool foo() = 0;
};
class B : public A
{
protected:
int state;
public:
int GetState() { return state; }
virtual bool foo()
{
// implementation which sets B::state to same value
}
}
Method B::foo() sets B::state variabl to some value. This variable may be
further used by other parts of code so its value is very important to be
vaild. The problem is that someone may construct class C like follows:
class C : public B
{
public:
virtual bool foo()
{
// implementation which does not set variable 'state'
}
}
In effect B::state has invalid value and some other parts of code whichuses
B::state may crash. How do i block function foo so class C will not be able
to override it ? Is it possible in C++ ? Or is some other workaround to
problem i mentioned ?

Add a comment (either in the code or in the documentation) that if
someone overrides the method they must call B::foo(). It works wonder in
a number of frameworks I've used. After all, if an object of type C does
not work correctly unless they call B::foo() then I'm pretty sure
they'll want to do that.

--
Erik Wikström
You have lot of options -
1. Use constructor for initializing the variable
2. Don't use virtual.

- Bharath

Jun 21 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.