小编Mak*_*oto的帖子

在没有模板参数的类模板中使用类名

代码来自C++书籍,如下所示:

为什么这个公共成员Link*next没有typename参数?

template <typename E> class Link {
private:
    static Link<E>* freelist;
public:
    E element;
    Link* next;  // this line confused me....

    Link(const E& elemval, Link* nextval = NULL)
    {
        element = elemval; next = nextval;
    }
    Link(Link* nextval = NULL) { next = nextval; }
    void* operator new(size t){
        if (freelist == NULL) return ::new Link;
        Link<E>* temp = freelist;
        freelist = freelist->next;
        return temp; // Return the link
    }
};
Run Code Online (Sandbox Code Playgroud)

我认为应该是Link<E>* next.

请告诉我它没有模板参数的原因.

c++ templates

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

如何通过字符串调用方法?

我可以通过组合字符串来调用方法来处理数据吗?

例如,输入data.image.truecolor()代码是可以的吗?

data.image.truecolor() # This line is successful to call method
Run Code Online (Sandbox Code Playgroud)

我的问题是:如果我有一个名为data的数据对象(不是字符串),如何将".image.truecolor"sting 组合到call方法来处理数据?

它像是:

result=getattr(data,".image.truecolor")
result() # which is equivalent to the code above
Run Code Online (Sandbox Code Playgroud)

当然,它失败了.我有一个AttributeError.

因为有许多方法可以处理数据,例如:

data.image.fog()
data.image.ir108()
data.image.dnb()
data.image.overview()
# .... and other many methods
Run Code Online (Sandbox Code Playgroud)

手工输入代码是愚蠢和丑陋的,不是吗?

出于这个原因,我希望我可以使用这段代码:

methods=["fog","ir108","dnb","overview"]
for method in methods:
    method=".image"+method
    result=getattr(data,method) # to call method to process the data
    result()  # to get the data processed
Run Code Online (Sandbox Code Playgroud)

这样做有可能吗?

python

6
推荐指数
2
解决办法
319
查看次数

如何在python中使用win32运行文件并获取hwnd?

我知道使用 os.startfile('....') 或 os.system('....') 可以运行文件,例如 *.pdf、*.mp4 等,但它不能获取该文件的 hwnd。(我必须知道 hwnd 来控制窗口,例如,移动、调整大小或关闭它)

当然,我可以通过win32gui.FindWindow(None,"file name")获取hwnd,但是如果有两个同名的窗口,就不能单独获取hwnd。

有没有一个函数可以运行一个文件并在win32中获取它的hwnd?

像这样:

hwnd=win32.function("file dir/file name") // run a file like os.startfile(...)

//hwnd=-1 if failed

//hwnd=1234567 if successful
Run Code Online (Sandbox Code Playgroud)

然后我可以运行多个文件并毫无问题地获取它们的 hwnd。

提前致谢。

python winapi

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

标签 统计

python ×2

c++ ×1

templates ×1

winapi ×1