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

pointers to member functions and virtual inheritance

helo

i'm going to use pointers to member function with virtual inheritance
in following way:

struct base {
public:
typedef void (base::*fun_t)(void);
int i;
void call(fun_t f) {
(this->*f)();
}
};

class derived : virtual public base {
public:
int j;
};

class derived2 : public derived {
public:
void fun() {
std::cout<<"1";
}
void callcall() {
call(reinterpret_cast<fun_t>(fun));
}

};

it requires reinterpret_cast in MSVC7.1 (and produces warning in g++),
but works.
is it ok? (i mean it will work under MSVC7.1, and maybe later, not
abstract portability and stadard conformance).
are there situations when it will fail?

is there some better _lightweight_ approach? (not involving heavy stuff
like boost::function -- it's definitely nice, but it's too much..)

(i've read article http://www.codeproject.com/cpp/FastDelegate.asp ,
but i didn't understand details enough to understand if it will really
work always).

with best regards, Alex 'killer_storm' Mizrahi.

Sep 12 '05 #1
7 1910
<al**********@gmail.com> schrieb im Newsbeitrag
news:11**********************@f14g2000cwb.googlegr oups.com...
i'm going to use pointers to member function with virtual inheritance
in following way:
struct base {
public:
typedef void (base::*fun_t)(void);
int i;
void call(fun_t f) {
(this->*f)();
}
};
class derived : virtual public base {
public:
int j;
};
class derived2 : public derived {
public:
void fun() {
std::cout<<"1";
}
void callcall() {
call(reinterpret_cast<fun_t>(fun));
}
};
it requires reinterpret_cast in MSVC7.1 (and produces warning in g++),
but works.
is it ok? (i mean it will work under MSVC7.1, and maybe later, not
abstract portability and stadard conformance).
are there situations when it will fail?
Look, I do not actually know, what you are doing. Where do you need any
descendant class?
reinterpret_cast is a dangerous way to do anything, but be safe.
is there some better _lightweight_ approach? (not involving heavy stuff
like boost::function -- it's definitely nice, but it's too much..)


What about STL mem_fun?

Best regards // oliver
Sep 12 '05 #2
al**********@gmail.com wrote:
i'm going to use pointers to member function with virtual inheritance
in following way: [snip] it requires reinterpret_cast in MSVC7.1 (and produces warning in g++),
but works.
is it ok? (i mean it will work under MSVC7.1, and maybe later, not
abstract portability and stadard conformance).
are there situations when it will fail?

[snip]

At the very least this approach seems quite confusing, if not
unportable or flat out wrong. I'd probably prefer redesigning if that's
an option. Those who look at your code later would thank you. If that's
not possible, what constraints force you into this approach? Perhaps we
can suggest an alternate path that will also meet your constraints.

Cheers! --M

Sep 12 '05 #3
> At the very least this approach seems quite confusing, if not
unportable or flat out wrong. I'd probably prefer redesigning if that's
an option.


well, i found odd things happen when i enable
pointers_to_members(full_generality), so i removed virtual inheritance,
and it became working without reinterpret_cast. instead inheriting from
class with virtual base i'm going to use it just as member, that's less
confusing and messy, i think..

as i understand, it's still is not perfectly correct to cast
pointer-to-member-of-derived to pointer-to-member-of-base, but it's
quite simple and should work always well.

Sep 12 '05 #4
<al**********@gmail.com> schrieb im Newsbeitrag
news:11**********************@g43g2000cwa.googlegr oups.com...
well, i found odd things happen when i enable
pointers_to_members(full_generality), so i removed virtual inheritance,
and it became working without reinterpret_cast. instead inheriting from
class with virtual base i'm going to use it just as member, that's less
confusing and messy, i think..
You do not know, what a virtual base class means.
"Virtual base classes offer a way to save space and avoid ambiguities in
class hierarchies that use multiple inheritance."

Where do you use multiple inheritance?
as i understand, it's still is not perfectly correct to cast
pointer-to-member-of-derived to pointer-to-member-of-base, but it's
quite simple and should work always well.


This is not a serious opinion, Sir.

Best regards // oliver

Sep 12 '05 #5
* al**********@gmail.com:

i'm going to use pointers to member function
There's almost _never_ any need for that.

with virtual inheritance
Why?

in following way:

struct base {
public:
typedef void (base::*fun_t)(void);
int i;
void call(fun_t f) {
(this->*f)();
}
};
There are no functions in 'base' that 'base::call' could call.

class derived : virtual public base {
public:
int j;
};
Huh.

class derived2 : public derived {
public:
void fun() {
std::cout<<"1";
}
void callcall() {
call(reinterpret_cast<fun_t>(fun));
}

};
Undefined behavior.
it requires reinterpret_cast in MSVC7.1 (and produces warning in g++),
but works.
is it ok?
No.

(i mean it will work under MSVC7.1, and maybe later, not
abstract portability and stadard conformance).
You mean -- will it seemingly "work" for some hypothetical non-conforming
compiler? That's impossible to say for us who don't have it. Try.

are there situations when it will fail?
Yes, it's undefined behavior and can fail in any situation.
is there some better _lightweight_ approach? (not involving heavy stuff
like boost::function -- it's definitely nice, but it's too much..)
Depends what you're trying to do. Have you considered:

class Base
{
private:
virtual void foo() = 0;
public:
void call() { foo(); }
};

class Derived2: public Base
{
private:
virtual void foo() { std::cout<<"1"; }
public:
void callcall() { call(); }
};

(i've read article http://www.codeproject.com/cpp/FastDelegate.asp ,
but i didn't understand details enough to understand if it will really
work always).


Are you trying to implement some kind of delegate functor?

--
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?
Sep 12 '05 #6
> You do not know, what a virtual base class means.
"Virtual base classes offer a way to save space and avoid ambiguities in
class hierarchies that use multiple inheritance." Where do you use multiple inheritance?


do you want to see full code repository? :)
if i didn't show code that use multiple inheritance, it doesn't mean
it's not used. i know what's it :)

Sep 13 '05 #7
> > is there some better _lightweight_ approach? (not involving heavy stuff
like boost::function -- it's definitely nice, but it's too much..)
Depends what you're trying to do. Have you considered:

class Base
{
private:
virtual void foo() = 0;
public:
void call() { foo(); }
};

class Derived2: public Base
{
private:
virtual void foo() { std::cout<<"1"; }
public:
void callcall() { call(); }
};
there might be more than one function in Derived2 that should be passed
to call -- call(&Derived2::foo), call(&Derived2::bar). this will not
work with simple virtual functions..

(i've read article http://www.codeproject.com/cpp/FastDelegate.asp ,
but i didn't understand details enough to understand if it will really
work always).

Are you trying to implement some kind of delegate functor?


yes. very simple degenerate case -- i do no have to pass "this" since
it's called in same class instance.
btw, how is boost::function and boost::bind implemented physically? is
there an article somewhere?

Sep 13 '05 #8

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

Similar topics

2
by: tekenenSPAM | last post by:
I know that one cannot cast a non-static member function to a void *. (http://users.utu.fi/sisasa/oasis/cppfaq/pointers-to-members.html) However, I have a case where I need to call a certain...
4
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived...
4
by: Ian Malone | last post by:
I have a class which looks like this: class reco { public: int height, width; double beta; double mu; simple_d_field estimate; simple_d_field auxiliary;
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
5
by: Martijn van Buul | last post by:
Hi. I'm having a peculiar problem at work. I've been googling for it, but haven't found an authorative answer. In a nutshell (Long story follows), what I'd like to know is: If I have a C++...
3
by: Ernesto Bascón | last post by:
Hi everybody: I have two questions: 1. I'm using opaque pointers in my classes to hide their data structures; there is a way to use opaque pointers in template classes; since the...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
14
by: v4vijayakumar | last post by:
Why we need "virtual private member functions"? Why it is not an (compile time) error?
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
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
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.