gpt4 book ai didi

c++ - 使用通用基础和布局有效的 C++ 来转换非多态结构吗?

转载 作者:行者123 更新时间:2023-11-28 06:12:26 25 4
gpt4 key购买 nike

以下代码是否适用于 C++?否则,有没有一种有效的方法可以同时将内存解释为不同类型的值?

#include <cstdio>
struct Base { int payload; };
struct D1 : Base { void operator()(){ printf("D1: %d\n", payload);} };
struct D2 : Base { void operator()(){ printf("D2: %d\n", payload);} };

int main()
{
D1 d1;
D2& d2 = static_cast<D2&>(static_cast<Base&>(d1));
d1();
d2();
d2.payload = 3;
d1();
d2();
}

回应@NickoPo:我的用例基本上如下。想象一下 IntBase 复制起来不一定便宜,有许多复杂的算法,其中一些算法从数字中获益于素数与奇数,而另一些则不然:

#include <cassert>
#include <cstdio>

bool is_odd(int value) { return 0 != value % 2; }
bool is_small_prime(int value) { return 2 == value || 3 == value || 5 == value || 7 == value; }

class IntBase
{
public:
explicit IntBase(int value) : m_value(value) {}
int value() const { return m_value; }
protected:
int m_value;
};

class OddInt : public IntBase
{
public:
explicit OddInt(int value) : IntBase(value) { assert(is_odd(m_value)); }
};

class SmallPrimeInt : public IntBase
{
public:
explicit SmallPrimeInt(int value) : IntBase(value) { assert(is_small_prime(m_value)); }
};

bool is_constrainable_to_odd_int(IntBase const& x)
{
return is_odd(x.value());
}

OddInt const& constrain_to_odd_int(IntBase const& x)
{
assert(is_odd(x.value()));
return static_cast<OddInt const&>(x);
}

bool is_constrainable_to_small_prime_int(IntBase const& x)
{
return is_small_prime(x.value());
}

SmallPrimeInt const& constrain_to_small_prime_int(IntBase const& x)
{
assert(is_small_prime(x.value()));
return static_cast<SmallPrimeInt const&>(x);
}

void algorithm(IntBase const&)
{
printf("algoritm(IntBase const&)\n");
}

void algorithm(OddInt const&)
{
printf("algoritm(OddInt const&)\n");
}

void algorithm(SmallPrimeInt const&)
{
printf("algoritm(SmallPrimeInt const&)\n");
}

void test(IntBase const& x)
{
if (is_constrainable_to_small_prime_int(x))
{
algorithm(constrain_to_small_prime_int(x));
}
else if (is_constrainable_to_odd_int(x))
{
algorithm(constrain_to_odd_int(x));
}
else
{
algorithm(x);
}
}

void test(OddInt const& x)
{
if (is_constrainable_to_small_prime_int(x))
{
algorithm(constrain_to_small_prime_int(x));
}
else
{
algorithm(constrain_to_odd_int(x));
}
}

int main()
{
IntBase x(0);
OddInt y(1);
OddInt z(7);

test(x); // algoritm(IntBase const&)
test(y); // algoritm(OddInt const&)
test(z); // algoritm(SmallPrimeInt const&)
}

相关:

最佳答案

如果你打算在使用类似接口(interface)的同时使用类型作为保证进行转换,我建议你只用你的新对象类型包装内部数据,然后提供对内部数据的访问以便传输它从一种类型到另一种类型。如果您不打算安全地执行静态转换或重新解释转换,则没有意义。

这是一个例子:

http://coliru.stacked-crooked.com/a/40d5efeff22fcdcd

#include <iostream>

//Base data structure to encapsulate only data.
struct data {
data(int i) : i(i) {}

int i;
};

//Wrapper around our data structure, with interfaces to access
//the values and the data; implement your own constructor to
//gate the value
class PrimeInt {
public:
PrimeInt(const int i)
: d(i) {}

PrimeInt(const data& other)
: d(other) {}

PrimeInt(data&& other)
: d(std::move(other)) {}

PrimeInt& operator=(const PrimeInt&) = default;
PrimeInt& operator=(PrimeInt&&) = default;

int get() {return d.i;};

operator data() {return d;};

private:
data d;
};

//Wrapper around our data structure, with interfaces to access
//the values and the data; implement your own constructor to
//gate the value
class OddInt {
public:
OddInt(const int i)
: d(i) {}

OddInt(const data& other)
: d(other) {}

OddInt(data&& other)
: d(std::move(other)) {}

OddInt& operator=(const OddInt&) = default;
OddInt& operator=(OddInt&&) = default;

int get() {return d.i;};

operator data() {return d;};

private:
data d;
};

//Notice that we can now implicitly cast from one type to another.
int main() {
PrimeInt pi(10);

std::cout << pi.get() << std::endl;

OddInt oi(pi);

std::cout << oi.get() << std::endl;

return 0;
}

关于c++ - 使用通用基础和布局有效的 C++ 来转换非多态结构吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30988081/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com