Topic: Why doesn't auto_ptr define an operator->*() ?
Author: "Paul Mensonides" <pmenso57@home.com>
Date: Sat, 5 May 2001 12:16:26 GMT Raw View
<u477539062@spawnkill.ip-mobilphone.net> wrote in message
news:l.988921532.1971313476@[63.69.198.25]...
: I came across a situation recently where I would have liked auto_ptr
: to define an operator->*(). I was writing a template function object
: class that should work with any pointer type. The class is similar to
: std::mem_fun_t except it works with any pointer-like type rather than
: just plain pointers.
[snip]
: So, I did a little research and I found that there was a suggestion on
: this forum back in 1995 to have operator->*() included in the standard
: auto_ptr. Does anyone know why it didn't make it?
Off the top of my head, because the semantics of overloading operator->* are
non-generic and asinine. (this is my guess)
for instance:
-------------------------------------
#include <iostream>
// simple little class X
struct X {
// these functions all print out that they were called and what there args
were...
void f(void) {
std::cout << "f()" << std::endl;
return;
}
void g(void) {
std::cout << "g()" << std::endl;
}
void f(int a) {
std::cout << "f(" << a << ")" << std::endl;
}
void g(int a) {
std::cout << "g(" << a << ")" << std::endl;
}
void f(int a, int b) {
std::cout << "f(" << a << ", " << b << ")" << std::endl;
}
void g(int a, int b) {
std::cout << "g(" << a << ", " << b << ")" << std::endl;
}
};
// beginning of asinine b.s.
class Xfunctor { // base functor
protected:
X& m_x;
Xfunctor(X& x) : m_x(x) {
return;
}
};
class X_void : public Xfunctor { // handler for void (X::*)(void)
private:
void (X::* m_mptr)(void);
public:
X_void(X& x, void (X::* mptr)(void)) : Xfunctor(x), m_mptr(mptr) {
return; }
void operator()(void) {
(m_x.*m_mptr)();
return;
}
};
class X_int : public Xfunctor { // handler for void (X::*)(int)
private:
void (X::* m_mptr)(int);
public:
X_int(X& x, void (X::* mptr)(int)) : Xfunctor(x), m_mptr(mptr) {
return; }
void operator()(int a) {
(m_x.*m_mptr)(a);
return;
}
};
class X_int_int : public Xfunctor { // handler for void (X::*)(int, int)
private:
void (X::* m_mptr)(int, int);
public:
X_int_int(X& x, void (X::* mptr)(int, int)) : Xfunctor(x), m_mptr(mptr)
{ return; }
void operator()(int a, int b) {
(m_x.*m_mptr)(a, b);
return;
}
};
// fake 'auto_ptr' type class
template<class T> class Y { // i.e. auto_ptr
private:
T* m_t;
public:
Y(void) : m_t(new T) { return; }
~Y(void) {
delete m_t;
}
X_void operator->*(void (X::* mptr)(void)) {
return X_void(*m_t, mptr);
}
X_int operator->*(void (X::* mptr)(int)) {
return X_int(*m_t, mptr);
}
X_int_int operator->*(void (X::* mptr)(int, int)) {
return X_int_int(*m_t, mptr);
}
};
// driver
int main() {
void (X::* p)(int, int) = &X::f;
Y<X> a;
(a->*p)(4, 5);
p = &X::g;
(a->*p)(6, 7);
return 0;
}
-------------------------------------
You *should* be able to overload operator->* just like operator-> if you want
(i.e. return a pointer)
for example:
// begin elegant non-C++ example...
-------------------------------------
template<class T> class AutoPtr {
private:
T* m_pT;
public:
T* operator->*() { // NO ARGUMENTS
return m_pT;
}
};
int main() {
AutoPtr<X /* from above */ > ax;
void (X::* px)(int) = &X::f;
(a->*px)(5); // ((a->*())->*px)(5)
return 0;
}
-------------------------------------
*significantly* more elegant.
Paul Mensonides
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: u477539062@spawnkill.ip-mobilphone.net
Date: Thu, 3 May 2001 20:34:17 GMT Raw View
I came across a situation recently where I would have liked auto_ptr
to define an operator->*(). I was writing a template function object
class that should work with any pointer type. The class is similar to
std::mem_fun_t except it works with any pointer-like type rather than
just plain pointers.
When I tried to use the class with auto_ptr I discovered the following:
struct foo {
void bar();
};
typedef void (foo::*foo_mem_ptr)();
foo_mem_ptr bar_ptr = &foo::bar;
foo* foo_ptr = new foo;
std::auto_ptr<foo> foo_auto_ptr(new foo);
(foo_ptr->*bar_ptr)(); // this works
(foo_auto_ptr->*bar_ptr)(); // this doesn't
So, I did a little research and I found that there was a suggestion on
this forum back in 1995 to have operator->*() included in the standard
auto_ptr. Does anyone know why it didn't make it?
Chris Hines
The sooner you start coding, the longer it will take you to finish.
--
Sent by chines from comscore in area com
This is a spam protected message. Please answer with reference header.
Posted via http://www.usenet-replayer.com/cgi/content/new
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]