- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
#include <iostream>
namespace detail
{
class CRWO;
class CRO;
class CWO;
class CO;
}
typedef detail::CRWO& CRWO;
typedef detail::CRO& CRO;
typedef detail::CWO& CWO;
typedef detail::CO& CO;
class C
{
friend class detail::CRWO;
friend class detail::CRO;
friend class detail::CWO;
friend class detail::CO;
public:
C( int a ) : value(a) {}
~C() {};
operator CRWO() { return *static_cast<detail::CRWO*>(static_cast<void*>(this)); }
operator CRO() { return *static_cast<detail::CRO*>(static_cast<void*>(this)); }
operator CWO() { return *static_cast<detail::CWO*>(static_cast<void*>(this)); }
operator CO() { return *static_cast<detail::CO*>(static_cast<void*>(this)); }
private:
void set( int newValue ) { value = newValue; }
int get() { return value; }
private:
int value;
};
class detail::CRWO
{
public:
void set( int newValue ) { static_cast<C*>(static_cast<void*>(this))->set( newValue ); }
int get() { return static_cast<C*>(static_cast<void*>(this))->get( ); }
operator ::CRO() { return *static_cast<detail::CRO*>(static_cast<void*>(this)); }
operator ::CWO() { return *static_cast<detail::CWO*>(static_cast<void*>(this)); }
operator ::CO() { return *static_cast<detail::CO*>(static_cast<void*>(this)); }
private:
CRWO(); CRWO(const CRWO&);~CRWO();CRWO& operator=(const CRWO&);void operator&(); void operator*();
};
class detail::CWO
{
public:
void set( int newValue ) { static_cast<C*>(static_cast<void*>(this))->set( newValue ); }
operator ::CO() { return *static_cast<detail::CO*>(static_cast<void*>(this)); }
private:
CWO(); CWO(const CWO&);~CWO();CWO& operator=(const CWO&);void operator&(); void operator*();
};
class detail::CRO
{
public:
int get() { return static_cast<C*>(static_cast<void*>(this))->get( ); }
operator ::CO() { return *static_cast<detail::CO*>(static_cast<void*>(this)); }
private:
CRO(); CRO(const CRO&);~CRO();CRO& operator=(const CRO&);void operator&(); void operator*();
};
class detail::CO
{
public:
private:
CO(); CO(const CO&);~CO();CO& operator=(const CO&);void operator&(); void operator*();
};int main(int argc, char *argv[])
{
C c(3);
CRWO rwo = c;
CRO ro = c;
CWO wo = c;
CO o = c;
std::cout << rwo.get() << std::endl;
wo.set( 5);
std::cout << ro.get() << std::endl;
return 0;
}
tirinox 02.02.2013 15:29 # 0
ErmineMD 02.02.2013 17:07 # +2
tirinox 02.02.2013 21:23 # +2
Psionic 02.02.2013 16:49 # +2
defecate-plusplus 02.02.2013 16:51 # +4
LispGovno 02.02.2013 19:13 # +1
carsten 28.02.2013 19:10 # +2
bormand 28.02.2013 19:54 # +2
bormand 02.02.2013 19:23 # +2
LispGovno 02.02.2013 19:36 # +1
Так же вроде нежелательно?
dynamic_cast<void*>(this) должно быть
absolut 02.02.2013 20:24 # 0
LispGovno 02.02.2013 22:12 # 0
absolut 04.02.2013 14:18 # 0
bormand 02.02.2013 23:00 # +1
Для случаев со множественным наследованием - да. Для обычных будет одинаковый результат.
C++98, 5.2.7. Dynamic cast, пункт 7: If T is “pointer to cv void,” then the result is a pointer to the most derived object pointed to by v.
Пример: http://ideone.com/IGn0Yt
LispGovno 03.02.2013 01:54 # 0
LispGovno 08.02.2013 08:56 # 0
T const (&) - что это означает? Как это работает вообще?
defecate-plusplus 08.02.2013 10:12 # 0
absolut 08.02.2013 10:21 # +1
defecate-plusplus 08.02.2013 10:46 # +2
guest 19.02.2013 14:15 # +1
roman-kashitsyn 20.02.2013 08:07 # 0
LispGovno 28.02.2013 21:07 # −1
roman-kashitsyn 28.02.2013 21:49 # +2
An rvalue of type “pointer to cv1 void” can be converted to an rvalue of type “pointer to cv2 T,” where T
is an object type and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1. A value of
type pointer to object converted to “pointer to cv void” and back to the original pointer type will have its
original value
LispGovno 01.03.2013 06:44 # 0