假设我有一个模板化的类
template <typename T> struct Node
{
// general method split
void split()
{
// ... actual code here (not empty)
}
};
Run Code Online (Sandbox Code Playgroud)
需要在Triangle类案例中专门研究这一点
template <>
struct Node <Triangle*>
{
// specialise the split method
void split() {}
} ;
Run Code Online (Sandbox Code Playgroud)
但我不希望再次改写整个模板!唯一需要改变的是split()
方法,仅此而已.
例:
struct Foo { Foo() { printf("foo\n"); } };
static Foo foo;
__attribute__((constructor)) static void _bar() { printf("bar\n"); }
Run Code Online (Sandbox Code Playgroud)
它是确定性的foo
还是bar
首先印刷的?
(我希望并且期望静态对象的构造函数总是先执行但不确定,并且GCC关于构造函数属性的文档没有说明任何内容.)
如何在C/C++中执行RGB-> YUV转换?
我有一些Bitmap .. RGB我需要将它转换为YUV
库?TUTS?文章?
是否存在任何构建函数或替代简单快速的转义C
字符数组,如果与例如一起使用,printf
应该再次产生原始字符数组.
char* str = "\tHello World\n";
char* escaped_str = escape(str); //should contain "\tHello World\n" with char \ ,t.
printf(escaped_str); //should print out [TAB]Hello World[nextline] similar to if str was printed.
Run Code Online (Sandbox Code Playgroud)
c中是否有一种简单的方法来转义带有c转义字符的字符串.
我有缓冲区包含一个带转义字符的字符串.我想要包含在C文件中.为此,我需要逃避它,以便它可以被遵守.我只需要知道是否有简单的方法来做它而不是扫描缓冲区\n \n等,并生成c escape字符.
for(int i=0; i< strlen(buffer);i++)
if(buffer[i]=='\n')
sprintf(dest,"\\n")
else ....
Run Code Online (Sandbox Code Playgroud)
我写了这个函数.它工作正常.
char* escape(char* buffer){
int i,j;
int l = strlen(buffer) + 1;
char esc_char[]= { '\a','\b','\f','\n','\r','\t','\v','\\'};
char essc_str[]= { 'a', 'b', 'f', 'n', 'r', 't', 'v','\\'};
char* dest = (char*)calloc( l*2,sizeof(char));
char* …
Run Code Online (Sandbox Code Playgroud) 我正在尝试新标准的lambda表达式,但仍然不太了解它们.
假设我的代码中有一个lambda,例如在我的主代码中:
int main( int argc, char * argv[])
{
//some code
[](int x, int y)->float
{
return static_cast<float>(x) / static_cast<float>(y);
};
//some more code here
//<---now I want to use my lambda-expression here
}
Run Code Online (Sandbox Code Playgroud)
很明显我可能需要多次使用它,所以答案"只是在那里定义它"不起作用:P所以,我如何在代码中稍后调用这个lambda表达式?我是否必须创建一个函数指针并使用该指针?还是有更好/更简单的方法?
我正在寻找类似于在Python中遍历字符串的东西:
即
for char in str:
do something
Run Code Online (Sandbox Code Playgroud)
我怎么能用C++做到这一点?
谢谢
如何通过包含与另一个包含的另一个头文件同名的头文件来创建解决方案?
例如:
// src/blah/a.hpp
#ifndef A_HPP
#define A_HPP
namspace blah
{
class A
{
}
}
#endif
// src/blah/b.hpp
#ifndef B_HPP
#define B_HPP
#includes "a.hpp"
namspace blah
{
class B
{
}
}
#endif
// src/foo/a.hpp
#ifndef A_HPP
#define A_HPP
namspace foo
{
class A
{
}
}
#endif
// src/foo/c.hpp
#ifndef C_HPP
#define C_HPP
#includes "../b.hpp"
#includes "a.hpp" // won't be included due to multiple inclusion prevention
namspace foo
{
class C
{
}
}
#endif
Run Code Online (Sandbox Code Playgroud)
在最后一个头文件中,由于多个包含预处理器保护,因此不会包含a.hpp.真的,这应该没问题,因为类在不同的命名空间中.我意识到,简单的方法是只更改foo/a.hpp的名称或者只是在多重包含守护中给它一个假名.有没有更好的办法?
编辑 我理解你可以通过在#define和#ifndef指令中使用更具描述性的名称来解决这个问题(例如FOO_A_HPP和BLAH_A_HPP),但我想知道这是推荐的还是最好的方法.有些人会建议使用不同的文件名作为更好的解决方案,还是不重要?你会建议使用这个约定: …
从c ++开始,注意到你可以用两种方式初始化变量
int example_var = 3; // with the assignment operator '='
Run Code Online (Sandbox Code Playgroud)
要么
int example_var(3); // enclosing the value with parentheses
Run Code Online (Sandbox Code Playgroud)
是否有理由使用一个而不是另一个?
c++ variable-assignment copy-constructor assignment-operator
我读了一本手册说(见http://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared):
此外,
f(shared_ptr<int>(new int(42)), g())
如果g抛出异常,可能导致内存泄漏.如果使用make_shared,则不存在此问题.
为什么会导致内存泄漏?
这是一些C++代码:
#include <iostream>
using namespace std;
class m
{
public:
m() { cout << "mother" << endl; }
};
class n : m
{
public:
n() { cout << "daughter" << endl; }
};
int main()
{
m M;
n N;
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
mother
mother
daughter
Run Code Online (Sandbox Code Playgroud)
我的问题是我不想在创建N时调用m的构造函数.我该怎么办?