pointers to member functions and virtual inheritance

A

alex.mizrahi

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

Guest

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
 
M

mlimber

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
 
A

alex.mizrahi

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

Guest

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
 
A

Alf P. Steinbach

* (e-mail address removed):
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

alex.mizrahi

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 :)
 
A

alex.mizrahi

is there some better _lightweight_ approach? (not involving heavy stuff
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..

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?
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top