How to block member overridable

M

Marcin Prochownik

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

ajk

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

Amal P

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 ?

Are you trying to block the overriding or name hiding?
 
D

dasjotre

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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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

Bharath

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.

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

- Bharath
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top