需要帮助实施SmartPointer
Need help implementing a SmartPointer
本文关键字:SmartPointer 帮助 更新时间:2023-10-16
我在在C 中实现智能指针进行分配时遇到了困难。我的代码应该创建两个SmartPointers。第一个点为1,第二点最初为3,但已更改为10。我的代码应打印出
intone:1inttwo:10
,但它打印出
intone:10Inttwo:4919984
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <exception>
using namespace std;
template <typename T>
class SmartPointer
{
public:
SmartPointer(T in)
{
try
{
if(in<0)
{
throw "SmartPointer does not handle negative numbers";
}
T * val = ∈
this->data = val;
}
catch(exception& e)
{
cout << "Standard exception: " << e.what() << endl;
}
}
~SmartPointer()
{
delete data;
}
T getValue();
void setValue(T val);
private:
T*data;
};
template <typename T> T SmartPointer<T>::getValue()
{
return *this->data;
}
template <typename T> void SmartPointer<T>::setValue(T val)
{
if(val<0)
{
throw "SmartPointer does not handle negative numbers";
}
this->data = &val;
}
int main()
{
SmartPointer<int> intOne(1);
SmartPointer<int> intTwo(3);
intTwo.setValue(10);
cout<<"intOne: "<<intOne.getValue()<<endl;
cout<<"intTwo: "<<intTwo.getValue()<<endl;
}
您的构造函数按值t值t,然后尝试将内部指针设置为该值的本地副本。它应该取为t的地址。
您的异常内容是不必要的,因为该类甚至不知道哪种类型是T,因此可能是某种类型的类型,该类型没有与INT的比较操作员。即使您确实需要抛出异常,也应该将它们捕获在main()
中,因为没有人期望构造函数将错误消息打印到屏幕上。throw "message"
是错误的,因为您正在尝试投掷const char*
实例。而是调用标准异常之一,例如throw domain_error("message")
。
使用this->
除了使键入较慢外,其他任何事情。
如果您不知道,使用endl
会冲洗缓冲区。如果您不需要,请使用' n'。
固定代码:
//#include <stdio.h> Why?
//#include <stdlib.h>
#include <iostream>
#include <exception>
using namespace std;
template<typename T>
class SmartPointer
{
public:
SmartPointer(T *in);
~SmartPointer();
T getValue();
void setValue(const T &val);
private:
T *data;
};
template<typename T> SmartPointer<T>::SmartPointer(T *in) //Moved implementation outside of class declaration
{
//Completely unnecessary exception stuff
data = in; //this-> is unnecessary
}
template<typename T> SmartPointer<T>::~SmartPointer()
{
delete data;
}
template<typename T> T SmartPointer<T>::getValue()
{
return *data;
}
template<typename T> void SmartPointer<T>::setValue(const T &val)
{
//More unnecessary exception stuff
*data = val;
}
int main()
{
SmartPointer<int> intOne(new int); //The constructor should take a pointer for the object to store instead of an value
SmartPointer<int> intTwo(new int);
intOne.setValue(1);
intTwo.setValue(10);
cout << "intOne: " << intOne.getValue() << 'n'; //endl flushes the buffer, kind of unnecessary here
cout << "intTwo: " << intTwo.getValue() << 'n'; //since it gets flushed upon the program exiting anyways
}
相关文章:
- 需要帮助设置在C++中使用的Potrace
- 在指针的帮助下,文本文件中单词的频率
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 如何在Qbutton的帮助下更改Q对话框的宽度
- 需要帮助将结构数组传递给函数
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 有人可以帮助我处理正则表达式吗?
- C++调用具有 *this 属性的单个帮助程序函数
- C++:需要帮助了解运算符重载错误
- 需要以下代码的帮助,下面的代码有什么问题
- CS1 项目帮助C++
- 用于检查值是否为其任何参数的帮助程序函数
- 需要有关此 if 语句的帮助
- 类型限定宏帮助程序
- CoreCLR 中的检测探查器 - 将帮助程序程序集加载到 dotnet 进程的方法
- NS3 插槽混淆(需要帮助理解)
- 设计帮助 - 为不同类型的消息处理通用接口的设计模式
- 需要帮助查找内存泄漏
- 需要帮助实施SmartPointer