Программы на C++. Форум разработчиков
-
Anonymous
Как получить строковое представление указателя на члена данных?
Сообщение
Anonymous »
У меня есть следующее, которое не компилируется: < /p>
Код: Выделить всё
#include
#include
#include
#include
struct A
{
int m_foo;
int m_bar;
};
struct BBase
{
BBase(std::uintptr_t ptr)
:
m_ptr(ptr)
{}
std::uintptr_t m_ptr;
};
template
struct B : public BBase
{
using AType = std::remove_cvref_t;
B(const AType a)
:
BBase(reinterpret_cast(M)),
m_a(a)
{
}
void Assign(A& a)
{
a.*M = m_a;
}
AType m_a;
};
using AFoo = B;
using ABar = B;
int main()
{
std::unordered_map map;
A a{.m_foo = 1, .m_bar = 4};
auto aFoo = new AFoo(2);
auto aBar = new ABar(2);
map[std::to_string(aFoo->m_ptr)] = aFoo;
map[std::to_string(aBar->m_ptr)] = aBar;
assert(map.size() == 2);
aFoo->Assign(a);
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79382955/how-do-i-get-a-string-representation-of-a-pointer-to-a-data-member[/url]
1737690617
Anonymous
У меня есть следующее, которое не компилируется: < /p>
[code]#include
#include
#include
#include
struct A
{
int m_foo;
int m_bar;
};
struct BBase
{
BBase(std::uintptr_t ptr)
:
m_ptr(ptr)
{}
std::uintptr_t m_ptr;
};
template
struct B : public BBase
{
using AType = std::remove_cvref_t;
B(const AType a)
:
BBase(reinterpret_cast(M)),
m_a(a)
{
}
void Assign(A& a)
{
a.*M = m_a;
}
AType m_a;
};
using AFoo = B;
using ABar = B;
int main()
{
std::unordered_map map;
A a{.m_foo = 1, .m_bar = 4};
auto aFoo = new AFoo(2);
auto aBar = new ABar(2);
map[std::to_string(aFoo->m_ptr)] = aFoo;
map[std::to_string(aBar->m_ptr)] = aBar;
assert(map.size() == 2);
aFoo->Assign(a);
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79382955/how-do-i-get-a-string-representation-of-a-pointer-to-a-data-member[/url]