我看到C++中的某些函数被声明为
virtual const int getNumber();
Run Code Online (Sandbox Code Playgroud)
但是如果函数声明如下,有什么区别?
const virtual int getNumber();
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别?
我正在尝试使用prctl( PR_SET_NAME, "procname", 0, 0, 0)为进程设置名称,当我阅读有关PR_SET_NAME的Linux手册时,如果我正确理解它,它看起来像设置了线程的名称.
可以prctl用来设置进程名称吗?如何设置进程名称?
如果我们将复制构造函数和赋值运算符设置为私有并且不提供任何实现,则它们将被禁用,如下所示:
class Test
{
priavate:
Test(const Test&);
Test& operator=(const Test&);
};
Run Code Online (Sandbox Code Playgroud)
但在这种情况下我们需要这样做?我的意思是我们什么时候应该这样做?
我是C ++的新手,我对继承中的c ++受保护成员和私有成员有疑问。
如果一个公共类继承了一个基类,那么受保护的私有成员变量是否将成为派生类的一部分?
例如:
class Base
{
protected:
int a;
int b;
private:
int c;
int d;
public;
int q;
};
class Derived: public Base
{
};
Run Code Online (Sandbox Code Playgroud)
派生类也有所有成员a, b, c, d, q吗?我们可以int a在派生类中将a定义为public,protected和private吗?
我有一个关于 C 函数如何返回静态变量的问题:
在data.h文件中:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int age;
int number;
} person;
person * getPersonInfo();
Run Code Online (Sandbox Code Playgroud)
在data.c
#include "data.h"
static struct person* person_p = NULL;
person * getPersonInfo()
{
person_p = (struct person*)malloc(10 * sizeof(struct person));
return person_p;
}
Run Code Online (Sandbox Code Playgroud)
在main.c
#include "data.h"
int main()
{
person* pointer = getPersonInfo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
函数getPersonInfo()返回一个指针,该指针是 中的静态指针data.c,这是允许且合法的吗?在 中main.c,该函数可以getPersonInfo()这样使用吗:person* pointer = getPersonInfo();
我想知道 C 函数的返回指针是好还是坏设计?如果这是一个不好的做法,那么在下面的例子中什么是好的做法:
问题是继续部分: c函数返回静态变量
在data.h文件中:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int age;
int number;
} person;
person * getPersonInfo();
Run Code Online (Sandbox Code Playgroud)
在 data.c
#include "data.h"
static struct person* person_p = NULL;
person * getPersonInfo()
{
person_p = (struct person*)malloc(10 * sizeof(struct person));
return person_p;
}
Run Code Online (Sandbox Code Playgroud)
在 main.c
#include "data.h"
int main()
{
person* pointer = getPersonInfo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
基本上,文件中的main函数main需要获取静态指针指向的数组所有元素的值person_p,如果这不是一个好的做法,那么好的做法应该是什么?
我在C中有一个函数:
void start_fun()
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
我想用来pthread_create()创建一个线程,启动例程是start_fun(),如果没有修改void start_fun(),如何获取函数指针start_fun();
我有以下代码:
// Example program
#include <iostream>
#include <string>
#include <typeinfo>
using namespace std;
class Name
{
int a;
int b;
};
class Name1
{
int a;
int b;
};
int main()
{
Name1* name1;
Name* name;
// trying to implement the following code:
// check if name1 is of type of pointer Name1 then do someting
// check if name is of type of pointer Name then do someting
}
Run Code Online (Sandbox Code Playgroud)
如何检查指针的类型name1?
Name1 和 Name 不是继承的,dynamic_cast 不能用,那么如何检查指针的类型呢?
我有一个包含一些静态函数的 C 文件,如何使用 google test 来测试那些静态函数?
头文件:
test.h
int accessData();
Run Code Online (Sandbox Code Playgroud)
源文件:
test.c
static int value;
static int getData()
{
return value;
}
int accessData()
{
if(value != 0)
{
return getData();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
静态函数被全局函数调用,但是如何使用谷歌测试来测试那些静态函数?
我有以下secrets.yaml的templetes在头盔图表:
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
USER_NAME: YWRtaW4=
PASSWORD: MWYyZDFlMmU2N2Rm
Run Code Online (Sandbox Code Playgroud)
我需要建立在不同的命名空间相同的秘密,例如,命名空间test1,test2,test3,test4,如何与同一指定不同的命名空间secrets,因此同样secret可以在不同的命名空间中产生的呢?
我对 C 中的 return 语句有一个疑问,它真正从哪里返回:
int base(int a)
{
if(a == 1)
return 0;
}
int inherit()
{
base(1);
// the rest of the code
}
Run Code Online (Sandbox Code Playgroud)
因此,在该inherit()函数中,base()被调用,并执行return 0, 在本例中;其余代码inherit()仍然执行吗?return 语句实际上是如何工作的?
我有以下代码尝试使用Optional类:
import java.util.Optional;
// one class needs to have a main() method
public class HelloWorld
{
public String orelesMethod() {
return "hello";
}
public void test() {
String value;
value = Optional.ofNullable(null).orElse(orelesMethod());
System.out.println(value);
}
// arguments are passed using the text field below this editor
public static void main(String[] args)
{
HelloWorld hello = new HelloWorld();
hello.test();
}
}
Run Code Online (Sandbox Code Playgroud)
编译时说:
incompatible types: Object cannot be converted to String
value = Optional.ofNullable(null).orElse(orelesMethod());
Run Code Online (Sandbox Code Playgroud)
我找不到问题所在,有人可以帮忙吗?
谢谢!