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

Detecting member function constness

Hello:

Is it possible to detect programmatically the constness of a member
function when it is called?

That is, I would like to see a generic implementation (i.e. it does not
depend on the class Base) of the createInstance function below that
produces the output:

Output:
a const method was called.
a non const method was called.

Code:
class Base {
public:
void constMethod() const {
}
void nonConstMethod() {
}
};

void main() {
Base &base = createInstance<Base>();
base.constMethod();
base.nonConstMethod();
}
Thanks:
Querejeto

Aug 28 '05 #1
6 1845
Querejeto wrote:
Hello:

Is it possible to detect programmatically the constness of a member
function when it is called?

That is, I would like to see a generic implementation (i.e. it does not
depend on the class Base) of the createInstance function below that
produces the output:

Output:
a const method was called.
a non const method was called.


Like so ?

struct A
{
bool IsConst() const
{
return true;
}

bool IsConst()
{
return false;
}

};

#include <iostream>

int main()
{

A a;
const A & ac = a;

std::cout << a.IsConst() << "\n";
std::cout << ac.IsConst() << "\n";

}
Aug 28 '05 #2
* Querejeto:

Is it possible to detect programmatically the constness of a member
function when it is called?

That is, I would like to see a generic implementation (i.e. it does not
depend on the class Base) of the createInstance function below
You forgot to show that function.

that produces the output:

Output:
a const method was called.
a non const method was called.

Code:
class Base {
public:
void constMethod() const {
}
void nonConstMethod() {
}
};
The member functions need to be virtual if they're to be overridden in
derived classes.

void main() {
Not valid.

Base &base = createInstance<Base>();
Not valid. Perhaps you meant

Base* base = createInstance<Base>();

base.constMethod();
base.nonConstMethod();
}


The question doesn't make sense.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 28 '05 #3
Alf P. Steinbach wrote:
* Querejeto:

Is it possible to detect programmatically the constness of a member
function when it is called?

That is, I would like to see a generic implementation (i.e. it does not
depend on the class Base) of the createInstance function below
You forgot to show that function.


The function call is in the code snippet that I presented. It is the
first line of the main method. Too bad I could not highlight it to make
it more clear. A prototype would be, I guess:

template <typename T>
T& createInstance();
that produces the output:

Output:
a const method was called.
a non const method was called.

Code:
class Base {
public:
void constMethod() const {
}
void nonConstMethod() {
}
};


The member functions need to be virtual if they're to be overridden in
derived classes.


Agree. Assume they are virtual.
void main() {


Not valid.


why is that prototype not valid?
Base &base = createInstance<Base>();
Not valid. Perhaps you meant

Base* base = createInstance<Base>();


Why are you requiring the method to return a pointer? I don't quite see
why a reference won't do. In fact, I could argue that it conforms to
the standard that references are always valid, while pointers could be
NULL.
base.constMethod();
base.nonConstMethod();
}


The question doesn't make sense.


To give you a little bit of more context of what I am trying to find
out: I would like to think of the class Base as a plain business class
(I guess Java programmers like to call it POJO: Plain Old Java Object)
that knows nothing of how it is used.

If clients so desire, they could attach extra functionality to it, like
for example: clients may want to log the object state after a non-const
method is called, or clients may want the object to notify observers
that it state changed, etc.

That sounds like there are cross-cutting concern that I would like to
extract from the class implementation, instead of inheriting
functionality from multiple classes (which seems intrusive to me) or
applying policies, which is also code that tends to "contaminate" the
pure business logic on the class Base.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


I apologize in advance if my posting does not conform to the standard.
I am an occasional user of newsgroups.

Aug 28 '05 #4
* Querejeto:
void main() {
Not valid.


why is that prototype not valid?


Because the standard says so. It's not valid for C, not for C++, and has
never been valid. The result type of 'main' must be 'int'.

Base &base = createInstance<Base>();


Not valid. Perhaps you meant

Base* base = createInstance<Base>();


Why are you requiring the method to return a pointer? I don't quite see
why a reference won't do. In fact, I could argue that it conforms to
the standard that references are always valid, while pointers could be
NULL.


OK, with the prototype & explanation you now supplied (snipped) it's
technically valid, sorry.

That sounds like there are cross-cutting concern that I would like to
extract from the class implementation


For general pre-call actions all you need is an ordinary smart-pointer
class. For general post-call actions the smart-pointer operator-> needs to
itself return a smart-pointer, where that operator-> result smart-pointer's
destructor (or a data member's destructor) does the post-call action.
However, while this works for simple logging, synchronization etc. you
cannot detect which member function is called because operator-> isn't told.

To do things on a per-member-function basis you need to use a wrapper that
offers the same external interface as Base, and forwards all calls to Base.

Such wrappers can be generated automatically by tools (use Google, perhaps
first to find the acronym for cross-cutting things, I always forget... ah,
aspect-oriented, first relevant hit and two links further, <url:
http://aosd.net/wiki/index.php?title=Tools_for_Developers>), or they can be
written "by hand"; in your case it's as simple as

class Derived: public Base
{
void constMethod() const
{
Logger log( whatever );
Base::constMethod();
}

void nonConstMethod()
{
Logger log( whatever );
Base::nonConstMethod();
}
};

where the constructor & destructor of Logger, which you must define to your
needs, does the logging work; here there's no "detection" of const or
non-const member function, but that information can be hardwired... ;-)

It's also possible to use more platform/compiler-dependent techniques that
within the constraint of a given platform are more general, e.g. COM-object
interceptors, but I'd advice against it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 28 '05 #5
Alf P. Steinbach wrote:

For general pre-call actions all you need is an ordinary smart-pointer
class. For general post-call actions the smart-pointer operator-> needs to
itself return a smart-pointer, where that operator-> result smart-pointer's
destructor (or a data member's destructor) does the post-call action.
However, while this works for simple logging, synchronization etc. you
cannot detect which member function is called because operator-> isn't told.


I believe this is exactly what I was looking for. I wonder if the
operator -> could tell whether the function being called was const or
not.

For example, suppose that my object is a subject (as in the
subject/observer pattern). If I want to notify its observers after
executing every non-const function in my business object, it would be
great if the smart pointer could tell whether the function being called
was const or not. That way I could inject the call to notify() at the
smart pointer abstraction level and remove the duplication of having to
do it on every non-const function on my business object.

Aug 30 '05 #6
* Querejeto:
Alf P. Steinbach wrote:

For general pre-call actions all you need is an ordinary smart-pointer
class. For general post-call actions the smart-pointer operator-> needs to
itself return a smart-pointer, where that operator-> result smart-pointer's
destructor (or a data member's destructor) does the post-call action.
However, while this works for simple logging, synchronization etc. you
cannot detect which member function is called because operator-> isn't told.


I believe this is exactly what I was looking for. I wonder if the
operator -> could tell whether the function being called was const or
not.


operator-> isn't told which member function, or even if this is a member
function call, which means it cannot do the detection. However, the client
code presumably knows, and can select one of two ways of calling the
function, e.g. like p->foo() versus p.modify()->bar(), and the smart-pointer
can then enforce at compile time that the const way, p->foo(), is not used
to call a non-const function, but AFAICS the opposite can not be enforced.
I think this is rather kludgy and non-transparent compared to a wrapper --
check out the aspect-oriented programming tools, there must be a
wrapper-generator there, somewhere -- but it's your choice.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 30 '05 #7

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

Similar topics

17
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
3
by: qWake | last post by:
The C++ language standard stipulates at section 9.4.1 that " A static member function shall not be declared const " The question is: what problem(s) could possibly exist in allowing static...
2
by: trying_to_learn | last post by:
while seeing an example i was surprised to see that a const member function is allowed to change a static data member as shown below. I am trying to reason why.... and my guess is that static data...
7
by: Srini | last post by:
Hello, Rules for inline functions say that they have to be defined in the same compilation unit as their declarations. For class member functions this means that the inline member functions must...
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
16
by: recover | last post by:
#include <string> #include <iostream> using namespace std; class TConst { private: string con; string uncon; public:
2
by: mimi | last post by:
Hi,all. The section 13.5.1 of the <C++ primer 3rd editionsays, a static member frunction may not be declared as const or volatile. I could not explain to myself why? The constness seems to be...
4
by: viki | last post by:
We have zillion of instances inf instance->m_member in the code. We are going introduce the 'accessors' Get() and Set() for m_member, and m_member going private (and renamed, too). We would like...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.