- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在开发一个小代码时遇到了一些问题。它本质上是一个几何库,我在其中为 3D 点定义了一个类,为 3D 表面定义了一个抽象类,然后我实现了不同的表面(平面、圆锥……)。该库是内置在模板中的(它必须是这样我才能在 ceres-solver 中使用它)。 MCVE这是:
点.h
#ifndef POINT_H
#define POINT_H
#include <iostream>
#include <memory>
template <typename T, int n>
class Surface;
template <typename T>
class Point
{
public:
typedef std::shared_ptr<Point<T>> Ptr;
typedef std::shared_ptr<const Point<T>> ConstPtr;
Point(T&& x, T&& y, T&& z)
{
data_[0] = x;
data_[1] = y;
data_[2] = z;
std::cout << "Point constructor" << std::endl;
}
Point(const T* data)
{
std::cout << "Cosntructor point from data ptr" << std::endl;
for (size_t i = 0; i < 4; i++)
data_[i] = data[i];
}
Point(const Point<T>& other) : Point(other.data_)
{
std::cout << "Constructor copy point" << std::endl;
}
~Point()
{
std::cout << "Point destructor" << std::endl;
}
template <int n>
T distance(const std::shared_ptr<const Surface<T, n>> surface) const
{
std::cout << "\n1: " << *this << std::endl;
ConstPtr a(this);
std::cout << "2: " << *this << std::endl;
T d = surface->distance(a);
std::cout << "3: " << *this << std::endl;
return d;
}
T& operator[](size_t i)
{
return data_[i];
}
T operator[](size_t i) const
{
return data_[i];
}
T& x()
{
std::cout << "x&" << std::endl;
return data_[0];
}
T& y()
{
return data_[1];
}
T& z()
{
return data_[2];
}
T x() const
{
return data_[0];
}
T y() const
{
return data_[1];
}
T z() const
{
return data_[2];
}
private:
T data_[3];
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const Point<T>& point)
{
os << "[";
for (size_t i = 0; i < 3; i++)
{
if (i != 0)
{
os << ",";
}
os << point[i];
}
os << "]";
return os;
}
#endif // POINT_H
表面.h
#ifndef SURFACE_H
#define SURFACE_H
#include <iostream>
#include <memory>
template <typename T>
class Point;
template <typename T, int n>
class Surface
{
public:
typedef std::shared_ptr<Surface<T, n>> Ptr;
typedef std::shared_ptr<const Surface<T, n>> ConstPtr;
Surface()
{
std::cout << "Surface constructor" << std::endl;
}
virtual ~Surface()
{
std::cout << "Surface destructor" << std::endl;
}
virtual T distance(const std::shared_ptr<const Point<T>> point) const = 0;
T& operator[](size_t i)
{
return data_[i];
}
T operator[](size_t i) const
{
return data_[i];
}
protected:
T data_[n];
};
#endif // SURFACE_H
plane.h
#ifndef PLANE_H
#define PLANE_H
#include "surface.h"
#include <math.h>
template <typename T>
class Point;
template <typename T>
class Plane : public virtual Surface<T, 4>
{
public:
typedef std::shared_ptr<Plane<T>> Ptr;
typedef std::shared_ptr<const Plane<T>> ConstPtr;
using Surface<T, 4>::data_;
Plane(T&& nx, T&& ny, T&& nz, T&& d)
{
data_[0] = nx;
data_[1] = ny;
data_[2] = nz;
data_[3] = d;
std::cout << "Plane constructor" << std::endl;
}
Plane(const T* data)
{
std::cout << "Cosntructor plane from data ptr" << std::endl;
for (size_t i = 0; i < 4; i++)
data_[i] = data[i];
}
Plane(const Plane<T>& other) : Plane(other.data_)
{
std::cout << "Constructor copy plane" << std::endl;
}
virtual ~Plane()
{
std::cout << "Plane destructor" << std::endl;
}
virtual T distance(const std::shared_ptr<const Point<T>> point) const override
{
return (data_[0] * point->x() + data_[1] * point->y() + data_[2] * point->z() - data_[3]) /
sqrt(pow(data_[0], 2) + pow(data_[1], 2) + pow(data_[2], 2));
}
};
template <typename T>
std::ostream& operator<<(std::ostream& os, const Plane<T>& plane)
{
os << plane[0] << "*x+" << plane[1] << "*y+" << plane[2] << "*z = " << plane[3];
return os;
}
#endif // PLANE_H
主要
#include "plane.h"
#include "point.h"
int main()
{
Point<float>::Ptr point(new Point<float>(100, 100, 1.6));
Plane<float>::Ptr plane(new Plane<float>(1, 2, 3, -10));
float d1 = plane->distance(point);
std::cout << "Distance from " << *point << " to " << *plane << " -> " << d1 << std::endl;
std::cout << "Point " << *point << "; Plane " << *plane << std::endl;
float d2 = point->distance<4>(plane);
std::cout << "Distance from " << *point << " to " << *plane << " -> " << d2 << std::endl;
std::cout << "Point " << *point << "; Plane " << *plane << std::endl;
return 0;
}
这会产生正确的距离输出。但是,如果我使用 Point::distance
方法x
和 y
该点的值更改为 0
而如果我使用 Plane::distance
, x
和 y
一点都没有改变。在这两种情况下都是 distance
方法是 const 并作为输入 const std::shared_ptr<const Plane<T>>
和 const std::shared_ptr<const Point<T>>
.
我的问题:如果所有内容都设置为 const
,那么 Point 的值怎么可能改变? ?
我知道当 ConstPtr a(this)
时会发生这种情况在 Point::distance
被破坏了,但我什至不知道使用哪个构造函数来创建 ConstPtr a(this)
在Ponit::distance
方法。
程序的输出是:
Point constructor
Surface constructor
Plane constructor
Distance from [100,100,1.6] to 1*x+2*y+3*z = -10 -> 84.1338
Point [100,100,1.6]; Plane 1*x+2*y+3*z = -10
1: [100,100,1.6]
2: [100,100,1.6]
3: [100,100,1.6]
Point destructor
Distance from [0,0,1.6] to 1*x+2*y+3*z = -10 -> 84.1338
Point [0,0,1.6]; Plane 1*x+2*y+3*z = -10
Plane destructor
Surface destructor
Point destructor
最佳答案
问题出在ConstPtr a(this);
- 这一行创建一个 std::shared_ptr
取得当前对象的所有权和 delete
超出范围后就可以了。
因此 point
在调用 float d2 = point->distance<4>(plane);
之后指针变成了一个悬挂指针并取消引用它会导致未定义的行为
为了解决问题 Point
类需要继承自 std::enable_shared_from_this
和行 ConstPtr a(this);
需要变成ConstPtr a(shared_from_this());
关于c++ - const std::shared_ptr<const T> 作为函数的参数最终会改变智能指针中类的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51921410/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!