C++避免重复声明的语法是什么
C++ What syntax for avoid duplicated declarations?
我正在学习C++,尤其是OO编程。
我的程序使用指针来处理动态内存分配。
在创建我的默认构造函数时,我对用重复自己感到无聊
myInt = new int;
myOtherInt = new int;
等等。。
所以我的问题是:有没有办法写这样的东西:
myInt, myOtherInt = new int;
这是我的构造函数代码:
Annonce::Annonce(string Titre, long double Prix, string Intro, string Description, vector<vector<string>> ImgUrls) {
titre = new string;
intro = new string;
description = new string;
imgUrls = new vector<vector<string>>;
prix = new long double;
id = new size_t;
*id = nbAnnonces;
*titre = std::move(Titre);
*prix = Prix;
*intro = std::move(Intro);
*description = std::move(Description);
*imgUrls = std::move(ImgUrls);
}
没有从动态内存分配多个变量的快捷方式。
用于分配内存的函数返回单个内存地址(指针(。每个变量在内存中都应该有自己的、唯一的位置。
C++语言的语法需要改变,以支持返回多个地址(指针(的动态内存函数。
一个建议是减少内存分配的数量。在从动态内存分配之前,问问自己:"我真的需要从动态内存中分配吗?"。
有没有一种方法可以写这样的东西:
myInt, myOtherInt = new int;
是的,有两种方式。但第一个比你具体要求的更一般。。。
1.比起引用类型,更喜欢值类型
如果你不知道这些是什么:值类型和引用类型(维基百科(。
在某些编程语言中,几乎所有的变量、函数参数、类字段等都是引用。Java就是一个突出的例子。但在C++中——尤其是现在——我们更喜欢值而不是引用,除非有充分的理由不直接使用值。值语义在该语言中很常用,也得到了很好的支持,而且大多数使用它们更容易。另请参阅:
为什么用C++11 编码时不建议使用指针
具体来说,这意味着,在定义结构或类时,我们将为其提供非指针成员。在您的情况下:
class Announcement {
public:
using url_type = std::string; // perhaps use a URL library?
using image_urls_container_type = std::unoredered_set<url_type>;
// I'm guessing the URLs aren't really ordered
std::string title;
std::string introduction;
std::string description;
std::vector<image_urls_container_type> image_urls;
// Are the images really a sequence of indices? Shouldn't this be something like
// std::unordered_map<image_id_type,image_urls_container_type> image_urls ?
long double prize;
std::size_t id;
Announcement() = delete;
// constructors here, if you like
}
现在,我并不是说这一定是最好的定义,也不是说它符合你的所有需求。但如果你这样定义它,你会得到:
- 无需使用
new
- 无需删除任何内容
- 没有内存泄漏的机会(由于此类(
2。否则,请使用std::unique_ptr
的
std::unique_ptr<T>
类可以让您避免自己管理内存——它可以处理这些问题。因此,如果你让你的成员成为std::unique_ptr
,你可以使用get()
初始化他们指向的值,例如:
void foo(int x) {
auto p = std::make_unique<p>();
p.get() = x;
// do stuff with p
// ...
// no need to delete p at the end of the function
}
第一个预回答建议:不要这样做。
第二个预回答建议:避免自我管理的内存分配/释放,使用STL容器和/或智能指针(std::unique_ptr
、std::shared_ptr
等(代替
答:不,据我所知,你不能写之类的东西
int * myInt, myOtherInt = new int;
其分配两个变量(具有不同的已分配指针(。
但是,您可以将指针包装在一个类或结构中,该类或结构会自动分配所包含的指针(并且,可能会在析构函数中销毁它(。
只是为了好玩。。。如果你写一个包装如下
template <typename T>
struct wrappPnt
{
T * pnt = new T{};
T & operator * ()
{ return *pnt; }
T const & operator * () const
{ return *pnt; }
~wrappPnt ()
{ delete pnt; }
};
你可以这样写你的Annonce
struct Annonce
{
wrappPnt<std::string> titre, intro, description;
wrappPnt<std::vector<std::vector<std::string>>> imgUrls;
wrappPnt<long double> prix;
Annonce (std::string Titre, long double Prix, std::string Intro,
std::string Description,
std::vector<std::vector<std::string>> ImgUrls)
{
*titre = std::move(Titre);
*prix = Prix;
*intro = std::move(Intro);
*description = std::move(Description);
*imgUrls = std::move(ImgUrls);
}
};
第一个帖子回答建议:不要这样做。
第二个答案建议:避免自我管理的内存分配/释放,使用STL容器和/或智能指针(std::unique_ptr
、std::shared_ptr
等(代替
- C++避免重复声明的语法是什么
- 使用基类指针调用基类的值构造函数的语法是什么?
- 这行/语法是什么意思?
- 初始化对象以在 C++08 中作为参数传递的首选语法是什么?
- 将引用绑定到指针的语法是什么?(各种)
- 这个typedef和转换运算符语法是什么意思
- 此语法是什么意思.(::*).
- 将显式指定的函数模板重载作为模板参数传递的正确语法是什么?
- 使用 pybind11 绑定 typedef 类型的正确语法是什么?
- 将此类传递给引用的正确语法是什么?
- 带有约束的可变参数模板的'requires'表达式的语法是什么?
- 在TensorFlow C++中用于feed_dict的语法是什么?
- 调用模板化类内的枚举类枚举器的正确语法是什么
- 返回没有 typedef 的成员函数指针的语法是什么?
- 将强制转换运算符重载到 std::map 的正确语法是什么
- 这种增量语法是什么意思
- 返回结构的方法的正确语法是什么
- C 中的(*)语法是什么意思
- 堆栈(int = 10),这个语法是什么意思(C++)
- variadic模板阵列参考参数的语法是什么?