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

How to block member overridable

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
5 2126
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: aaronh64 | last post by:
Have you ever noticed that when you look at an Interface Class definition (ie IDisposable), the methods or properties belonging to the class are defined as abstract (C#) or overridable (vb)? How...
16
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
9
by: Sameh Ahmed | last post by:
Hello there Is there a way through dotNet to check if a certain user is a member of a specific group? I use ADSI to get the memberships of the user then compare them to the group I want to check,...
9
by: Joe HM | last post by:
Hello - I am trying to figure out how I can define a constant in a base class and override it in a derived class. The following shows a little example ... Module Test Sub Main() Dim A As...
2
by: Joe HM | last post by:
Hello - I have a function in a base class that I want to use the shadow'ed member variable of the derived class. Here is the code ... Public Class cCaptureBase Protected Const cDummy As...
2
by: MDFS | last post by:
Hi, I was wondering if anyone has any information on the performance impact of using functions declared as overridable? To Explain more fully; By default every function created in VB is NOT...
3
by: Rob | last post by:
Hi all, I have a bit of a complicated question, hope we have an SQL guru out there that can help us solve this killer problem. Due to the size of SQL Database we have (largest in the US), we...
8
by: barcaroller | last post by:
I have a pointer to a memory block. Is there a way I can map a vector<Tto this memory block? I would like to take advantage of the powerful vector<T> member functions. Please note that I cannot...
0
by: rpeters74 | last post by:
Within a base object I want to encapsulate the call to the Validation Block. The following code block will not work. The method tag with SelfValidation is never called. If I were to take the base...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.