小编Ras*_*yak的帖子

无法理解静态行为

我写的

// In file t.h

#ifndef __t_h__
#define __t_h__
static int abc;
#endif
Run Code Online (Sandbox Code Playgroud)

-

//In   main.c
    #include <stdio.h>
    #include "t.h"
    int main()
    {
        abc++;printf("%d \n", abc);
        test();
    }
Run Code Online (Sandbox Code Playgroud)

- -

//In test.c

#include <stdio.h>
#include "t.h"

void test()
{
    abc++;
    printf("%d \n", abc);
}
Run Code Online (Sandbox Code Playgroud)

当我运行该项目,我发现outputabc is 1 and 1.但是当我改变它int abct.h.的输出abc = 1 and 2.为什么静态不会在控件到达test.c文件时保留该值.如果它不会保留那么为什么它不应该提供错误作为static variable can not be shared between/among文件?

c c++

2
推荐指数
1
解决办法
239
查看次数

getinstance()究竟在单身人士中做了什么?

我遇到过单身人士模式.我无法理解它们之间的区别

singletonobj.getinstance().dosomething() //1st one
Run Code Online (Sandbox Code Playgroud)

singletonobj.dosomething() //2nd one
Run Code Online (Sandbox Code Playgroud)

做了getinstance()什么,在第二种情况下没有做到这一点?

c++ singleton design-patterns

2
推荐指数
1
解决办法
6880
查看次数

如何禁用printf功能?

我有三个文件如下

Test.cpp

void helloworld()
{
    disable pf;
    pf.Disable();
    printf("No statement \n");
    }
int main()
{
    disable dis;
    helloworld();
    printf("Hello World");
    system("pause");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

disable.cpp

    #include "StdAfx.h"
    #include "disable.h"
    disable::disable(void)
    {#define printf(fmt, ...) (0)}
    disable::~disable(void)
   {}
   void disable::Disable()
   {
    #define printf(fmt, ...) (0)
    }
Run Code Online (Sandbox Code Playgroud)

disable.h

#pragma once
class disable
{
public:
    disable(void);
    ~disable(void);
    void Disable();
};
Run Code Online (Sandbox Code Playgroud)

执行后,我得到输出为No Statement Hello World.但我想two printf statements通过调用Disable function和禁用它们disable constructor..请帮助我为什么它不工作以及如何解决这个问题.请帮忙.

但是,如果我愿意,事情就可以了

main()
{
#define printf(fmt, ...) (0)
printf("Hello …
Run Code Online (Sandbox Code Playgroud)

c++ printf

2
推荐指数
2
解决办法
5753
查看次数

为什么我们需要memset始终为零?

每当我们使用时,memset我们将其设置为零.

为什么?为什么不用1或2或其他东西.

此外,将结构设置为0似乎工作但设置为1不起作用:

typedef struct abc{
    int a;
} abc;

int main()
{
    abc* ab;
    memset(ab, 0, sizeof(abc));// it sets abc->a = 0; correct
}
Run Code Online (Sandbox Code Playgroud)

但不是0如果我使用1像:

memset(ab, 1, sizeof(abc));
Run Code Online (Sandbox Code Playgroud)

然后是abc->a = garbage或的价值not equals to 1

为什么?

c c++

2
推荐指数
1
解决办法
4346
查看次数

为什么这个程序没有抛出任何错误?

我去采访了.采访者提出了这个问题.在看到这个问题之后,我告诉他将会有一个p未声明的错误.但是当我在我的电脑上运行该程序时,我对答案/输出感到惊讶,因为ink它也没有任何错误.请帮我理解这个问题.

#include <iostream>
using namespace std;
int main()
{
    static char *s[] = {"black", "white", "pink", "violet"};
    char **ptr[] = {s+3, s+2, s+1, s}, ***p;//Here
    p = ptr;
    ++p;
    cout << **p+1;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ pointers

2
推荐指数
2
解决办法
104
查看次数

为什么运算符重载的行为如此奇怪

你好朋友以下程序的输出是如此奇怪.我没理由.在此输入图像描述

#include <iostream>
using namespace std;

class xyz
{
    private:
    int ab, cd;
    public:
    xyz()
    {

    }
    xyz(int i, int j)
    {
        ab = i;
        cd = j;

    }
    xyz operator+(xyz);
    void show()
    {
        cout << ab << " ....  "<< cd;
    }

};
xyz xyz :: operator +(xyz ob)
{
    xyz temp;
    temp.ab = ab + temp.ab;
    temp.cd = cd + temp.cd;
    return temp;
}

int main()
{
   // xyz xy, yz;

   xyz xy(2, 3);
   xyz yz(4, 5);
    xy …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

1
推荐指数
1
解决办法
132
查看次数

通过链表实现Stack

我收到以下错误消息.无法解决它.谷歌搜索了很多.最后想到把它放在这里.

在此输入图像描述

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int stop;
struct stack
{
  int data;
  struct stack *next;
};
typedef struct stack *node, *top;
//typedef struct stack *top;
void push()
{
    int i;
    struct stack *x;
    x = malloc(sizeof(struct stack));
    printf("\n Enter the element your want to insert");
    scanf("%d", &i);
    x->data = i;
    x->next = top;
    top = x;
}
void pop()
{
    int i;
    if(top == NULL)
    {
        printf("\nStack is empty\n");
    }
    else{
    i = top->data;
    free(top);
    top = top->next; …
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
1035
查看次数

错误C2065:'errno':Visual Studio 2012中<string>中未声明的标识符

我有一个项目在运行Visual Studio 2010.现在我正在使用Visual Studio 2012.现在我在构建项目时遇到错误.我已经调查GoogleSO为好.但没有我得到任何解决方案.

    c:\program files\microsoft visual studio 11.0\vc\include\string(557): error C2065: 'errno' : undeclared identifier
2>c:\program files\microsoft visual studio 11.0\vc\include\string(557): error C2065: 'errno' : undeclared identifier
2>c:\program files\microsoft visual studio 11.0\vc\include\string(562): error C2065: 'errno' : undeclared identifier
2>c:\program files\microsoft visual studio 11.0\vc\include\string(574): error C2065: 'errno' : undeclared identifier
2>c:\program files\microsoft visual studio 11.0\vc\include\string(579): error C2065: 'errno' : undeclared identifier
Run Code Online (Sandbox Code Playgroud)

我曾尝试与包括stdlib.hsting头文件,但还是我收到此相同的错误.

任何帮助赞赏.

c c++ string visual-studio-2010 visual-studio-2012

1
推荐指数
1
解决办法
5660
查看次数

如何使asignment操作符重载工作以进行连续分配

今天我接受了采访.我被要求编写一个使赋值运算符重载.

假设我有3个对象,例如

className obj1, obj2, obj3;

现在我想这样分配

obj1 = obj2 = obj3;

怎么做?

我在下面编写了一个程序,但它正在抛出错误 error: no match for ‘operator=’ in ‘ab = ab1.overload::operator=((* & ab2))’

#include <iostream>

using namespace std;
class overload{
public:
    int x, y;
    overload operator=(overload &);
    overload(){x = 1; y = 2;}
};
overload overload::operator=(overload &ov)
{
    overload o;
    o.x = ov.x;
    o.y = ov.y;
    cout << o.x << "..." << o.y << endl;
return o;
}
int main()
{
    overload ab, ab1, ab2;
    ab = ab1 …
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
139
查看次数

为什么strcmp失败了?

我很遗憾在这个门户网站上提出这个问题.因为这可能是一个愚蠢的问题.但是,我正在努力找出为什么strcmp不起作用?我也写了我自己的strcmp,我发现s1并且rev不同于rev持有一些垃圾.请帮帮我哪里出错了?

void main()
{
int i,j;
 char rev[5];
 char s1[5]= "madam";
 for(i=strlen(s1)-1, j = 0; i>=0; j++, i--)
 {
  rev[j]=s1[i];
 }
 puts(rev);
 printf("\n");
 puts(s1);
if(strcmp(rev,s1) == 0)
{
printf("Palindrome!!! \n");

}
}
Run Code Online (Sandbox Code Playgroud)

而不是strcmp(rev, s1)我自己动手i_strcmp(rev, s1).

int i_strcmp(char *s1, char *s2)
{
    int i;
    for (i = 0; s1[i] && s2[i]; ++i)
    {
        /* If characters are same or inverting the 6th bit makes them same */ …
Run Code Online (Sandbox Code Playgroud)

c string codeblocks

1
推荐指数
1
解决办法
2319
查看次数