C++避免重复声明的语法是什么

C++ What syntax for avoid duplicated declarations?

本文关键字:语法 是什么 声明 C++      更新时间:2023-10-16

我正在学习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_ptrstd::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_ptrstd::shared_ptr等(代替