Implementing the swap in the copy and swap idiom

Following What is the copy and swap idiom and How to provide a swap function for my class, I tried implementing the swap function like in the latter accepted answer option number 2 (having a free function that calls a member function) instead of the direct friendly free function in the former link. However the following doesn't compile #include <iostream> // Uncommenting the followi

在复制和交换方式中实现交换

以下是什么是复制和交换习语以及如何为我的课程提供交换功能,我尝试实现交换功能,就像后面接受的答案选项2(具有调用成员函数的自由函数)而不是直接友好免费功能在前面的链接。 但以下内容不能编译 #include <iostream> // Uncommenting the following two lines won't change the state of affairs // class Bar; // void swap(Bar &, Bar &); class Bar { public: Bar(unsigned int bottles=0) : bottle

how do I call an inline friend function with the same name as a member function?

As described here C++11 style SFINAE and function visibility on template instantiation class member functions overshadow free functions. Using a fully qualified name usually works, however I am having a hard time with friend functions of other classes which are declared in-line. Consider the following example: namespace N { struct C { friend int f(const C& c) { ret

如何使用与成员函数相同的名称来调用内联朋友函数?

如此处所述,C ++ 11风格的SFINAE和模板实例化类成员函数上的函数可见性会遮盖自由函数。 使用完全限定的名称通常有效,但是我很难与其他类的声明为联机的朋友函数。 考虑下面的例子: namespace N { struct C { friend int f(const C& c) { return 1; } friend int g(const C& c) { return 2; } }; struct D { void f() {

Providing swap() for a C++ template class breaks std::swap()?

I was trying to implement the copy-and-swap idiom in my custom Matrix class, and I ran into some trouble with the implementation of swap() in the way suggested in the linked-to question: (The compiler I used is the one from MS VS2010 IDE, dialect is good old-fashioned C++03.) // matrix.h namespace my_space { template<typename T> class Matrix { public: /* ... */ friend void sw

为C ++模板类提供swap()会打破std :: swap()?

我试图在自定义Matrix类中实现copy-and-swap成语,并且按照链接问题中建议的方式执行swap()时遇到了一些问题: (我使用的编译器是MS VS2010 IDE的编译器,方言是老式的C ++ 03。) // matrix.h namespace my_space { template<typename T> class Matrix { public: /* ... */ friend void swap(Matrix<T> &first, Matrix<T> &second) { using std::swap; swap(fi

how to provide a swap function for my class?

What is the proper way to enable my swap in STL algorithms? 1) Member swap . Does std::swap use SFINAE trick to use the member swap . 2) Free standing swap in the same namespace. 3) Partial specialization of std::swap . 4) All of the above. Thank you. EDIT: Looks like I didn't word my question clearly. Basically, I have a template class and I need STL algos to use the (efficient

如何为我的课程提供交换功能?

在STL算法中启用我的swap的正确方法是什么? 1)会员swap 。 std::swap是否使用SFINAE技巧来使用成员swap 。 2)在相同的命名空间中独立swap 。 3) std::swap部分专业化。 4)以上全部。 谢谢。 编辑:看起来我没有清楚地说出我的问题。 基本上,我有一个模板类,我需要STL算法来使用我为该类编写的(高效)交换方法。 1)是正确使用swap 。 当您编写“库”代码并且想要在swap上启用ADL(依赖于参数的查找)时,

When should you use 'friend' in C++?

I have been reading through the C++ FAQ and was curious about the friend declaration. I personally have never used it, however I am interested in exploring the language. What is a good example of using friend ? Reading the FAQ a bit longer I like the idea of the << >> operator overloading and adding as a friend of those classes. However I am not sure how this doesn't break e

什么时候应该在C ++中使用“朋友”?

我一直在阅读C ++常见问题,并对friend声明感到好奇。 我个人从未使用过它,但我有兴趣探索这种语言。 什么是使用friend的好例子? 再读一遍FAQ,我喜欢<< >>操作符重载并添加为这些类的朋友的想法。 但我不知道这是如何不破坏封装。 什么时候这些例外可以保持在OOP的严格范围内? 首先(IMO)不要听那些说friend没用的人。 它是有益的。 在很多情况下,您将拥有不具有公开可用数据或功能的对象。 对于

>C++ Automatically cast void pointer into Type pointer in C++ in #define in case of type is not given (C

Hi! I've used the following C macro, But in C++ it can't automatically cast void* to type* . #define MALLOC_SAFE(var, size) { var = malloc(size); if (!var) goto error; } I know, I can do something like this: #define MALLOC_SAFE_CPP(var, type, size) { var = (type)malloc(size); if (!var) goto error; } But I don't want to rewrite a big portion of code, where M

> C ++在没有给出类型的情况下,自动将void指针转换为C ++中的类型指针(#define)(C

嗨! 我使用了下面的C宏,但是在C ++中它不能自动将void*为type* 。 #define MALLOC_SAFE(var, size) { var = malloc(size); if (!var) goto error; } 我知道,我可以做这样的事情: #define MALLOC_SAFE_CPP(var, type, size) { var = (type)malloc(size); if (!var) goto error; } 但我不想重写MALLOC_SAFE被使用的大部分代码。 有没有办法做到这一点,而不需要将类型赋予宏? 也许一些MSVC 20

Dynamic memory allocation in C++ class

I am sure there is an alternative solution for this problem but I would really like to understand why my solution is not working int n; class Segtree { public: int *s, size, h, *cl, *t; string a; int open, close, total; void init(string input,int siz) { size = siz; n = size; t = new int[size * 4]; cl = new int[size * 4]; s = new int[size *

C ++类中的动态内存分配

我确信有这个问题的替代解决方案,但我真的很想明白为什么我的解决方案无法正常工作 int n; class Segtree { public: int *s, size, h, *cl, *t; string a; int open, close, total; void init(string input,int siz) { size = siz; n = size; t = new int[size * 4]; cl = new int[size * 4]; s = new int[size * 4]; a = input; build(); }; }

Dynamically allocating a 2D array of object pointers in a class

I'm struggling at the moment with the idea of dynamically allocating arrays at runtime. Coming from Java, used to just declaring the arrays in the class skeleton and only needing the size in the implementation. This is what I've found to dynamically allocate 2D arrays: Grid.h Block** grid; Grid.cpp grid = new Block*[size] for(int i = 0 ; i < size ; i++) grid[i] = new Block[

在类中动态分配对象指针的二维数组

目前我正在努力在运行时动态分配数组。 来自Java,习惯于在类框架中声明数组,并且只需要实现中的大小。 这是我发现的动态分配二维数组的方法: Grid.h Block** grid; Grid.cpp grid = new Block*[size] for(int i = 0 ; i < size ; i++) grid[i] = new Block[size] 尽管处理对象我总是被告知,使用指向对象的指针而不是存储对象本身的性能明显要好得多,但这种方法很好。 所以当我试图让数组指针的第二维像这

C++ copy constructor for class with dynamically allocated array

Just starting on C++ (a few days now), I come with C background. I have a class that holds, mainly, an a pointer to an int array with the following code: class Array { private: int * _arr; int _size; public: Array(); Array(int size); Array(const Array& obj); // copy constructor ~Array(); void readInValues(); void mult(int num); void add(int num); vo

具有动态分配数组的类的C ++拷贝构造函数

从C ++开始(几天后),我带着C背景。 我有一个类,主要是一个指向int数组的指针,其代码如下: class Array { private: int * _arr; int _size; public: Array(); Array(int size); Array(const Array& obj); // copy constructor ~Array(); void readInValues(); void mult(int num); void add(int num); void printArray(); }; _arr是一个指向int数组的指针,当使用复制构

Pointer to array of pointers (dynamic allocation)

I am writing a card game in C++. I have a game class which keeps track of the players. I also have an abstract base class player, from which the classes person and computer derives. I would like to keep the players in an array. The number of players is unknown at compile time. Because some players are persons and other computers, I need a player pointer for each, stored in an array, which i

指向数组的指针(动态分配)

我正在用C ++编写一个纸牌游戏。 我有一个跟踪玩家的游戏课程。 我也有一个抽象的基础班级球员,班级人员和计算机从中派生出来。 我想保持阵容中的球员。 玩家人数在编译时是未知的。 因为有些玩家是个人和其他计算机,所以我需要一个玩家指针,存储在一个数组中,由于玩家的数量是未知数而被动态分配的,对吗? 由于我对C ++比较陌生,因此我无法弄清楚语法是如何寻找这种东西的。 对于动态数组,标准库提供了std::ve