据我所知,在C#中,如果没有另外标记,所有字段都是默认的私有字段.
class Foo
{
private string bar;
}
class Foo
{
string bar;
}
Run Code Online (Sandbox Code Playgroud)
我猜这两个声明是平等的.
所以我的问题是:我应该将私有变量标记为私有变量private?
好吧,我有这个原型是由C#中的其他人编写的,我试图把它放到Objective-C中.现在,我还没有任何关于C#的正式经验,所以我还不知道它的一切.我理解前三个变量是什么,但我遇到第四行和第五行(c_data)正在做的问题.是第四个声明一种方法,然后第五个定义它或正在发生什么?谢谢你的帮助!
public class c_data {
public double value;
public int label;
public int ID;
public c_data() { }
public c_data(double val) {
value = val;
}
}
Run Code Online (Sandbox Code Playgroud) 我收到此错误:
arthur@arthur-VirtualBox:~/Desktop$ gcc -o hw -ansi hw1.c
hw1.c: In function `main':
hw1.c:27:16: warning: assignment makes pointer from integer without a cast [enabled by default]
hw1.c: At top level:
hw1.c:69:7: error: conflicting types for `randomStr'
hw1.c:27:18: note: previous implicit declaration of `randomStr' was here
Run Code Online (Sandbox Code Playgroud)
在编译此代码时:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
char *rndStr;
rndStr = randomStr(FILE_SIZE);
/* Do stuff */
return 0;
}
/*Generate a random string*/
char* randomStr(int length)
{
char *result;
/*Do stuff*/
return result; …Run Code Online (Sandbox Code Playgroud) 在一个generate区块内,我有多个if陈述.当我在第一个if语句中声明一个连线时 - 我不能在其他if语句中使用它
请参阅我的模块的以下精简示例:
module my_module
#(parameter integer NUM_X_PORTS = 1,
parameter integer NUM_Y_PORTS = 1)
(
// port declarations
);
generate
if (NUM_X_PORTS > 0) begin
wire [NUM_X_PORTS-1:0] x1;
// logic filled in here
end
if (NUM_Y_PORTS > 0) begin
wire [NUM_Y_PORTS-1:0] y1;
// logic filled in here
end
if ((NUM_X_PORTS > 0) && (NUM_Y_PORTS > 0)) begin
for (i=0; i<NUM_Y_PORTS; i=i+1) begin
assign z[i] = y1[i] & |x1; // I can't use …Run Code Online (Sandbox Code Playgroud) 我正在使用GCC C为嵌入式产品开发硬件抽象库.在库中,有一个变量应该对链接库的应用程序是只读的,但可以在定义它的编译单元中进行修改.
是否有一种标准的,可接受的方式来声明允许应用程序读取变量中的值的整数(在库头文件中),但是如果尝试生成写回写的代码,则告诉编译器生成错误对吗?例如,如果我要将函数声明为:
extern void foo(int const bar);
Run Code Online (Sandbox Code Playgroud)
...然后允许调用者传递局部变量:
int bar = 0;
foo(bar);
Run Code Online (Sandbox Code Playgroud)
...但是如果函数声明尝试写入bar:
void foo(int const bar)
{
bar = 99;
}
Run Code Online (Sandbox Code Playgroud)
...然后编译器会报告错误:分配只读位置"bar".
放置const名称之前的语法似乎不会以与函数参数相同的方式应用于变量,并且下面的两行似乎实际上是等效的:
extern const int x;
extern int const y;
Run Code Online (Sandbox Code Playgroud)
...因为将y定义为int y;导致错误:'y'的冲突类型限定符,正如我所期望的那样,x被定义为int x;.
我知道我可以通过声明和定义一个返回值的访问器函数(它只能用作r值)来解决这个问题.
我在这里已经阅读了许多相关的问题,但是我找到的没有一个为C提供了明确的答案(而不是C++或C#):
有人可以指出我如何实现它的一个例子的方向,或者证实我怀疑它在语法上是不可实现的吗?
考虑一个类A,STL容器B和容器的二元谓词C.
容器B在类A中使用.但是类A也用在二元谓词C中.
struct C{
bool operator()(A const &a, A const &b){
return a.compare_variable < b.compare_variable;
}
};
Run Code Online (Sandbox Code Playgroud)
我们需要这个谓词来定义容器,它使用它来对元素进行排序.
因为容器声明变得相当长,所以我使用typedef来简化声明.
typedef B<A, vector<A>, C> type;
Run Code Online (Sandbox Code Playgroud)
最后,我的目标是在类A中声明容器B ---其声明缩写为"type"---即,作为静态公共成员变量.
class A{
public:
type container1, container2;
};
Run Code Online (Sandbox Code Playgroud)
什么是正确的顺序是宣布A,B和C?
我按顺序尝试了以下变化:
首先声明类A,然后是结构C,最后是typedef,我得到容器1,容器2没有命名类型的错误---类声明在类声明时不存在;
首先是typedef:错误加载---类和结构都没有定义;
我使用的方法是不必要的麻烦,是否存在更优雅的解决方案?
据我所知,C中的对象有3种类型的链接:1)外部2)内部和3)无,并且在块范围内声明的对象,如在函数体内,没有链接,除非前面有关键字"extern"或"静态".
但是为什么然后下面的函数声明能够链接到main()函数下面的定义,即使我在声明期间没有使用"extern"?请解释一下,因为它完全颠覆了我对这个主题的理解.谢谢.
#include<stdio.h>
int main()
{
int foo(); //working even though I've not used "extern"
printf("%d",foo());
}
int foo()
{
return 8;
}
Run Code Online (Sandbox Code Playgroud)
上述计划的结果:8
这是一个初学者的问题,但我没有找到一个全面的答案.
以下声明有何不同(如果有的话)?
CHARACTER(5) :: a
CHARACTER, DIMENSION (5) :: b
CHARACTER(LEN=5) :: c
CHARACTER :: d(5)
CHARACTER :: e*5
Run Code Online (Sandbox Code Playgroud)
并且:所有这些声明都可以与其他类型一起使用,例如REAL?
我在Youtube的一些视频中看过这个.
class Student {
private var _name: String!
private var _studentID: Int!
var name: String {
return _name
}
var studentID:Int {
return _studentID
}
init(name: String, studentID: Int) {
self._name = name
self._studentID = studentID
}
}
Run Code Online (Sandbox Code Playgroud)
他们为什么这样做(添加_name和_studentID)而不是:
class Student {
private var name: String!
private var studentID: Int!
init(name: String, studentID: Int) {
self.name = name
self.studentID = studentID
}
}
Run Code Online (Sandbox Code Playgroud)
非常感谢你.
我有两个模型Model,其子类ClientModel为环境模块。现在,我想ClientModel从所谓的接口声明一组属性Client。我该怎么做?我可以想象这样的事情:
interface Client {
name: string;
email: string;
}
declare class ClientModel extends Model implements Client {
// with name and email here without redeclare them
}
Run Code Online (Sandbox Code Playgroud)