我想创建一个具有以下类型的C++类:
使用此:想想GC的"Root"对象.
这在C++中是否可行?特别是,我正在使用g ++.愿意切换到铿锵声.模板或宏解决方案都很好.
谢谢!
class A{
private:
string a;
public:
A():a("hello world"){};
A(const string & arg):a(arg){};
void put()const{cout<<a;};
};
A aaa();
A bbb;
Run Code Online (Sandbox Code Playgroud)
那么,有什么之间的区别A aaa();和A bbb;是aaa一个功能?
我是C中的新手,并且在所有指针内容方面都存在一些问题.
我写了这段代码:
typedef struct edgeitem
{
double weight;
}EDGE_ITEM, *pEDGE_ITEM; //also declaration of a pointer
/************ edge that includes a pointer to the edge item, next and prev ******/
typedef struct edge
{
pEDGE_ITEM *edge_item;
pEDGE *next; //pointer to next edge
pEDGE *prev; //pointer to prev edge
}EDGE, *pEDGE;
Run Code Online (Sandbox Code Playgroud)
我这样得到一个错误,只是无法理解为什么.
我知道,edgeitem并且edge是标签,我可以使用struct edge *next,但我声明了指针那为什么我不能用呢?
如果我有指针,我需要使用*吗?
pEDGE_ITEM *edge_item
//or
pEDGE_ITEM edge_item
Run Code Online (Sandbox Code Playgroud)
我不明白,这是一个指针,为什么我要添加*?
最后一个问题是:如果我使用上述内容,有什么区别:
*EDGE next
EDGE *next
Run Code Online (Sandbox Code Playgroud)
最后一个:如果我要补充:
typedef struct edge_list
{
EDGE *head;
}EDGE_LIST;
Run Code Online (Sandbox Code Playgroud)
是这样的: …
我正在阅读'C编程语言',并遇到了关于struct的 typedef的问题.代码是这样的:
typedef struct tnode *Treeptr;
typedef struct tnode { /* the tree node: */
char *word; /* points to the text */
int count; /* number of occurrences */
struct tnode *left; /* left child */
struct tnode *right; /* right child */
} Treenode;
Run Code Online (Sandbox Code Playgroud)
到我们写的时候
typedef struct tnode *Treeptr;
Run Code Online (Sandbox Code Playgroud)
tnode仍未声明,但我们没有得到任何编译错误,但是当我们将上面的语句更改为:
typedef Treenode *Treeptr;
Run Code Online (Sandbox Code Playgroud)
我们得到编译错误:
error: parse error before '*' token
warning: data definition has no type or storage class
Run Code Online (Sandbox Code Playgroud)
是什么导致了差异?"struct tnode"与"Treenode"不一样吗?
我应该用
bool __declspec(dllexport) function()
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
要么
__declspec(dllexport) bool function()
{
return true;
}
Run Code Online (Sandbox Code Playgroud)
这两者有什么区别?这会影响函数的执行方式或从应用程序调用时返回的内容吗?(此代码适用于DLL).
我正在使用GeomagnetismLibrary,其中一个函数声明有一个格式
int MAG_robustReadMagModels(char *filename, MAGtype_MagneticModel *(*magneticmodels)[], int array_size)
Run Code Online (Sandbox Code Playgroud)
为了简单起见,我把它简化为专注于我的目标
void blah(int *(*a)[])
{
(*a)[0] = malloc(sizeof(int));
(**a)[0] = 12;
}
Run Code Online (Sandbox Code Playgroud)
如果我想调用这个函数,我必须声明一个变量,如:
int *a[1];
blah(&a);
Run Code Online (Sandbox Code Playgroud)
现在在我的情况下,不管什么a永远不会有多个元素,所以我不想声明a为数组,而只是像指针一样
int *a;
Run Code Online (Sandbox Code Playgroud)
有没有什么办法可以在调用时blah根据需要键入强制转换或取消引用这个变量而不会导致段错误?
另外,您如何根据类型转换定义该类型,例如:( int *[]*)?
谢谢
为什么java不允许在非静态内部类中使用静态初始化块进行静态声明?
在下面的代码中,尽管做了同样的事情,outer2将工作,而inner2将不会.有任何想法吗?我不是在寻找一种解决方法,我只是想了解为什么java无法做到这一点.
public class WhyUNoStatic {
public static final String outer1 = "snth"; // ok
public static final String outer2; // ok
static
{
outer2 = "snth";
}
public class Inner {
public static final String inner1 = "snth"; // still ok!
public static final String inner2; // FAILURE TIME
static
{
inner2 = "snth";
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:请注意,这inner1将正常工作.并不是java禁止内部类中的静态变量,它只是禁止声明它们.
当第一个声明被注释掉时,以下命名空间定义无法编译.如果第一个声明foo是取消注释的,那么它编译就好了.
namespace Y
{
//void foo();
void ::Y::foo(){}
}
Run Code Online (Sandbox Code Playgroud)
标准中的相关部分(§8.31)说:
当declarator-id被限定时,声明应引用先前声明的成员
据我所知,此规则可防止将名称引入其他名称空间.我想知道是否可以放宽该规则以允许引用当前命名空间的qualified-id.
我无法理解我在面试中遇到的这段代码声明。
int(*(*ptr[3])(char*))[2];
Run Code Online (Sandbox Code Playgroud)
我试着看一个IDE,但是我所拥有的只是它是数据类型的数组
int (*(*[3])(char *))
Run Code Online (Sandbox Code Playgroud)
我不明白这一点。
declaration ×10
c++ ×6
c ×3
pointers ×3
struct ×2
typedef ×2
definition ×1
java ×1
namespaces ×1
object ×1
qualifiers ×1
static ×1
syntax ×1
variables ×1
windows ×1