为了便于阅读,我喜欢在类声明中使用初始成员变量.在初始化过程中我也很习惯使用lambda(参见片段).我的问题是在成员函数的声明中使用这样的lambda函数有什么优点或缺点.
class foo
{
private:
int index_ = 5;
int value_ = [](int index)
{
int result = 0;
for( int i = 0;i <index; i++)
result += i;
return result;
}(index_ );
};
Run Code Online (Sandbox Code Playgroud)
编辑:as bar :: bar(int index):index_(index){}比bar :: bar(int index){index_ = index;}更优化,无论上面的代码表现更好还是只是可读性.
我正在实现一个单例类作为 c++14 中的练习,其中必须使用智能指针。此代码片段有效,但我想使用 make_unique ,不要与旧的 new 和 delete 关键字混淆,但它说它无法访问私有成员。为什么会发生?关键字“new”与智能指针一起使用是否安全?
#include <iostream>
#include <memory>
using namespace std;
class foo
{
private:
int val_;
private:
foo() {}
public:
// properties
void set(const int v) { val_ = v; }
int get() const { return val_; }
//methods
static const unique_ptr<foo>& get_instance()
{
static unique_ptr<foo> pfoo( new foo() );
//unique_ptr<foo> pfoo = make_unique<foo>() ; // Error - cannot access private member declared in class 'foo'
return pfoo;
}
};
int main()
{
foo::get_instance()->set(100); …Run Code Online (Sandbox Code Playgroud) 尝试使用CreateProcess启动ffmpeg。
问题:
1)不能dshow在命令行中使用。
2)带有STDIN管道的RTMP流不显示该流。
问题:
1)命令行中与CreateProcess api有关的提示是什么?
2)怎么回事?如何解决该问题?
此代码有效:
BOOL bSuccess = CreateProcess(NULL,
L"ffmpeg.exe -y -loop 1 -i kites.jpg out.mp4",
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&siStartInfo,
&piProcInfo);
Run Code Online (Sandbox Code Playgroud)
使用CreateProcess时失败dshow。但是,它在控制台中用作命令行。
BOOL bSuccess = CreateProcess(NULL,
L"ffmpeg.exe -y -loop 1 -i kites.jpg -f dshow -i audio=\"Stereo Mix(Realtek High Definition Audio)\" out.mp4",
NULL,
NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&siStartInfo,
&piProcInfo);
Run Code Online (Sandbox Code Playgroud)
编辑:(具有绝对路径,仍然没有运气)
std::wstring cmdArgslistSetChannel = L"ffmpeg.exe -y -loop 1 -i c:\test\kites.jpg -f dshow -i audio=\"Stereo Mix(Realtek High Definition Audio)\" out.mp4"; …Run Code Online (Sandbox Code Playgroud) 我想发送一个成员函数作为参数,但它不会编译.为什么代码不起作用?这就是我写的.如果我通过一个lambda而不是它有效.
void global_func(std::function<void(void)>f)
{
f();
}
class goo
{
public:
goo() { }
void func1()
{
std::function<void(void)> fp = &goo::func2; // get a pointer to the function func2 . Error here or next line
global_func( fp);
}
void func2(void)
{
}
};
void main()
{
goo g1;
g1.func1();
}
Run Code Online (Sandbox Code Playgroud)
这里编译输出(我的程序名是tryvector.cpp)
1>------ Build started: Project: TryVector, Configuration: Debug Win32 ------
1> TryVector.cpp
1>e:\program files\microsoft visual studio 12.0\vc\include\functional(506): error C2664: 'void std::_Func_class<_Ret,>::_Set(std::_Func_base<_Ret,> *)' : cannot convert argument 1 from '_Myimpl *' to …Run Code Online (Sandbox Code Playgroud) 这似乎是一个非常愚蠢的问题,所以请耐心等待。我在程序中使用智能指针代替原始指针。建议我不要使用原始指针或尽可能混合两者。我也明白这一点。我也知道只有在必要时才应使用指针。
class Foo{
private: int val;
public:
Foo(int v) :val(v){}
int getvalue() const { return val; }
};
std::shared_ptr<Foo> foo = std::make_shared(Foo(10));
int v;
//Option I
v=foo->getvalue();
//Option II
v=foo.get()->getvalue();
Run Code Online (Sandbox Code Playgroud)
我觉得选项 I 更正确,因为选项 II 使用原始指针。但是使用原始指针在这里可能不会造成伤害,因为我没有分配或取消分配。
我经常对这两个选项感到困惑。哪一个更可取?它们只是一样吗?谢谢。
这是我的玩具程序。它无法编译并显示错误消息“没有运算符<<与这些操作数匹配”。任何帮助将不胜感激。
struct foo {
std::string name;
};
std::ostringstream& operator<<(std::ostringstream& os, const foo& info)
{
os << info.name;
return os;
}
void insert(const foo& info)
{
std::ostringstream os;
os << "Inserted here " << info; // don't work here
}
int main()
{
foo f1;
f1.name = "Hello";
insert(f1);
}
Run Code Online (Sandbox Code Playgroud) 我能够编译并运行它。如何在主作用域之外编写try-catch块,这似乎违背了我的逻辑?有什么术语可以描述这种行为?
int main() try
{
}
catch(...){}
Run Code Online (Sandbox Code Playgroud)