小编Mac*_*Mac的帖子

友元类不能调用私有构造函数

下面的代码是我今天正在使用的东西的简化版本。它有 2 个类 A 和 B。 B 类尝试使用 A 类的私有构造函数,但失败了。如果我将构造函数公开,则代码编译得很好。这是为什么?

#include <vector>

class A
{
friend class B;
private:
    A(int * int_ptr) {
        m_int = *int_ptr;
    }
private:
    int m_int;
};


class B
{
friend class A;
public:
    static void create_vec_a() {
        int v1(1);
        int v2(2);

        std::vector<int *> int_ptr_vec{
            &v1,
            &v2
        };

        std::vector<A> a_vec(int_ptr_vec.begin(),
            int_ptr_vec.end());
    }
};

int  main(int argc, char *argv[])
{
    B::create_vec_a();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我在 Visual Studio 中得到的错误是:

'A::A': cannot access private member declared in …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

2
推荐指数
1
解决办法
2331
查看次数

Python setter也返回值

我想知道是否可以有一个属性设置器来返回一个值。下面的代码试图从表面上解释问题。

基本上,我需要为对象设置一个属性,但是在我需要检查它是否唯一之前。我想知道是否有一种方法可以直接将唯一名称返回给用户,而无需在此后查询对象的新名称。

class MyClass(object):
    def __init__(self,ID):
        self._ID = ID
    @property
    def Name(self):
        return DBGetName(self._ID)

    @Name.setter
    def Name(self, value):
        UniqueName = DBGetUniqueName(self._ID,value)
        return DBSetName(UniqueName)

Myinstance = MyClass(SomeNumber)
#What I do now
Myinstance.Name = "NewName"
Uniquename = Myinstance.Name

#What I was wondering if possible. Line below is syntactically invalid, but shows the idea.
Name = (Myinstance.Name = "NewName")
Run Code Online (Sandbox Code Playgroud)

编辑:这是一个伪代码,我忘了实际将值传递给内部函数。我的错。

python setter decorator getter-setter python-3.x

1
推荐指数
1
解决办法
2185
查看次数

c ++返回函数类型作为其输入的函数

我正在学习c ++我想知道是否有一种方法来定义一个模板,其中返回类型实际上是函数输入的函数.

例如:

calling fun(1) would return me an int
calling fun(2) would return me a float
Run Code Online (Sandbox Code Playgroud)

我想这可以用某种地图来完成?

1 <> int
2 <> float
Run Code Online (Sandbox Code Playgroud)

我试图解决的问题是,例如,如果我有一个名为room的对象,我想要一个名为get_contents的函数,我将传递一个enum来定义返回类型.例如:

std::vector<Table> tables = room.get_contents(Room::TABLE);
std::vector<Chair> chairs = room.get_contents(Room::CHAIR);
Run Code Online (Sandbox Code Playgroud)

第一个问题可能不是这个问题的最佳解决方案,但我想知道是否可能.另外,做我想做的最好的模式是什么?

c++ c++11

1
推荐指数
1
解决办法
80
查看次数

用于定义类型的宏

在C++中有没有办法根据预处理器标志定义类型?我有两个版本的库,一个是本机的,一个是建立在thirft的顶部.

两个版本具有完全相同的函数签名,但返回和参数类型除外.本机版本传递对象的指针,而thirft版本使用i64来表示指针.

我想知道是否有办法构建一个项目,可以使用任何基于编译标志的库.例如:

//native
AType* obj = fun(AnotherType* another_obj)

//thirft
i64 obj = fun(i64 another_obj)
Run Code Online (Sandbox Code Playgroud)

我想知道是否有这样的构造:

if native
    X is Atype*
    Y is AnotherType*
else
    X is i64
    Y is i64
end

//native
X obj = fun(Y another_obj)

//thirft
X obj = fun(Y another_obj)
Run Code Online (Sandbox Code Playgroud)

c++ build c++11

0
推荐指数
1
解决办法
57
查看次数

不能push_heap列表<int>,适用于矢量

我想push_heap上一个list<int>,但编译失败抱怨的列表迭代器.如果我将列表更改为向量,它可以正常工作.

看看cpp引用我无法弄清楚为什么列表迭代器的行为会有所不同.也许有人可以解雇一些?

https://ideone.com/pk5iJG

#include <algorithm>
#include <functional>
#include <list>

using namespace std;

int main() {

    list<int> l;
    l.push_back(0);
    push_heap(l.begin(), l.end(), greater<int>());
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误:

In file included from /usr/include/c++/6/bits/stl_pair.h:59:0,
                 from /usr/include/c++/6/utility:70,
                 from /usr/include/c++/6/algorithm:60,
                 from prog.cpp:1:
/usr/include/c++/6/bits/stl_heap.h: In instantiation of ‘void std::push_heap(_RAIter, _RAIter, _Compare) [with _RAIter = std::_List_iterator<int>; _Compare = std::greater<int>]’:
prog.cpp:13:48:   required from here
/usr/include/c++/6/bits/stl_heap.h:200:28: error: no match for ‘operator-’ (operand types are ‘std::_List_iterator<int>’ and ‘int’)
       _ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
                            ^
In file …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
2
解决办法
91
查看次数

标签 统计

c++ ×4

c++11 ×3

build ×1

decorator ×1

getter-setter ×1

python ×1

python-3.x ×1

setter ×1