在C++中 - 假设派生类派生自基类,并且在基类中存在派生类重写的虚方法.有人能告诉我一个真实生活场景,虚拟函数的派生类版本可能需要调用虚函数的基类版本吗?
例,
class Base
{
public:
Base() {}
virtual ~Base() {}
virtual void display() { cout << "Base version" << endl; }
};
class Derived : public Base
{
public:
Derived() {}
virtual ~Derived() {}
void display();
};
void Derived::display()
{
Base::display(); // a scenario which would require to call like this?
cout << "Derived version" << endl;
}
Run Code Online (Sandbox Code Playgroud) 我想将CTRL-A(0x01)存储在C++字符串中.尝试以下,但它不起作用.你能说出我在这里失踪了吗?
string s = "\u0001";
Run Code Online (Sandbox Code Playgroud)
我用g ++编译时得到错误:
error: \u0001 is not a valid universal character
Run Code Online (Sandbox Code Playgroud) 虽然我使用像这样的迭代器,
//include header files
using namespace std;
int main()
{
map<int,int> intIntMap;
map<int,int>::iterator pos;
pos = intIntMap.begin();
intIntMap[0] = 1;
intIntMap[3] = 5;
intIntMap[4] = 9;
intIntMap[5] = 5;
//??
cout << (*pos).first << endl;
while( pos != intIntMap.end() )
{
cout << pos->first << " <---> " << pos->second << endl;
pos++;
}
}
Run Code Online (Sandbox Code Playgroud)
输出为4;
但是我使用像这样的迭代器:
//include header file
using namespace std;
int main()
{
map<int,int> intIntMap;
map<int,int>::iterator pos;
intIntMap[0] = 1;
intIntMap[3] = 5;
intIntMap[4] = 9;
intIntMap[5] …Run Code Online (Sandbox Code Playgroud) 在我的项目中,我有一个假设的场景:
3)在childClass Init函数中,我dynamic_cast用来将IFlow的objet转换为BaseClass,如下所示:
void ChildClass::init()
{
IFlow* pFlow = someMethod(); //it returns the IFlow object pointer
//this works for static cast but fails for dynamic cast
BaseClass *base = dynamic_cast<BaseClass*>(pFlow) ;
}
Run Code Online (Sandbox Code Playgroud)在上面的代码中,第二行dynamic _cast返回零,但如果dynamic_cast更改为static_cast则代码按预期工作.请指教
所以这是我的原始代码:
#include <iostream>
using namespace std;
int main ()
{
float x;
cout << "Please enter an integer value: ";
cin >> x;
if ((x >= 100) && (x < 200)) {
cout << "split";
} else if (x == 0 ||x == 1 ) {
cout << "steal";
} else {
cout << "split";
}
system("pause");
}
Run Code Online (Sandbox Code Playgroud)
它工作得很好,但我需要它以这种方式运行:
C:\> program.exe 109
Run Code Online (Sandbox Code Playgroud)
它会读取109并给出输出 - "steal".
C:\> program.exe 0.5
Run Code Online (Sandbox Code Playgroud)
它会读取0.5并给我输出"split".
我需要添加到原始代码中才能执行此操作?
gcc 4.7.2
c89
Run Code Online (Sandbox Code Playgroud)
你好,
#define LOG_ERR(fmt, ...) \
fprintf(stderr, "[ERROR] %s:%d: error [%s] " fmt "\n", __func__, __LINE__, strerror(errno), ##__VA_ARGS__)
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
LOG_ERR("Failed to connect to message queue [ %d ]", msg_id);
Run Code Online (Sandbox Code Playgroud)
fmt已在fprintf语句中连接.这怎么可能?
我试着用下面的代码来测试这个概念,但是因为编译错误而失败:
/* Using char array */
const char name[] = "Joe";
printf("Hello how " name " how are you today?\n");
Using constant string literal
const char *name = "Joe";
printf("Hello how " name " how are you today?\n");
Run Code Online (Sandbox Code Playgroud)
两个游戏我都有以下错误:
expected ')' before name
Run Code Online (Sandbox Code Playgroud)
非常感谢任何建议,
在C++中朋友函数/类的真正用途是什么?你能给出一个只有friend正确方法的例子吗?
谢谢
打印以下代码 -10
int x = 10;
-x;
cout << -x << endl; // printf("%d\n", -x);
Run Code Online (Sandbox Code Playgroud)
在C和C++编译器中都有(gcc 4.1.2).我期待第二行的编译器错误.可能是一些基本的东西,但我不明白这种行为.有人可以解释一下吗?
谢谢
我有一个执行图像匹配的 Visual C++ 程序。我正在使用 openCV。我希望在 Linux 服务器上运行 exe。但我不知道如何在linux下编译Visual C++代码?
任何人都可以在这方面帮助我吗?。。
我正在处理遗留代码,其中内存分配/释放以传统 C 风格完成,但希望使用自定义删除器将其包装在 unique_ptr 中。考虑这样一种情况:旧代码通过调用 calloc/malloc 来分配二维数组。我需要调用相应的旧版释放器函数,该函数带有两个参数 - 指向数组的指针和数组的大小。在这种情况下,如何定义一个带有 2 个参数的自定义删除器,这两个参数仅在初始化 unique_ptr 时传递以获取已分配数组的所有权?提前致谢。
下面是一个简化的(但人为的)示例:
// function allocates memory and returns size of the buffer
void legacyFunction(char **array, int *num)
{
char *p = (char *)calloc(5, sizeof(char));
for (auto i=0; i<5; ++i)
{
p[i] = 'a';
}
*num = 5;
*array = p;
return;
}
int main()
{
char *array = nullptr;
int numofElts = 0;
legacyFunction(&array, &numofElts);
// unique_ptr here to take ownership of array
// custom deleter should take …Run Code Online (Sandbox Code Playgroud)