小编Rak*_*kib的帖子

c ++ const成员函数

我正在阅读一本名为"Effective C++,Second Edition"的书,以及它关于const成员函数以及你如何具有逐位常量和概念常数的讨论.

它说大多数编译器都会使用按位常量,这是因为你无法改变const成员函数内对象的数据成员.

然后是一个成员函数的例子,它似乎在const测试中没有按位.

它是这样的:

#include "stdafx.h"
#include <string>
#include <iostream.h>

using namespace std;

class mystring
{

public:
    mystring(const char* value);

    operator char *() const { return data; }

private:
    char * data;
};

mystring::mystring(const char * value)
{

    mystring::data = const_cast<char*>(value);
}


int main(int argc, char* argv[])
{
    const mystring s = "Hello";

    char * nasty = s;

    *nasty = 'M';

    printf("s: %c", s);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

当它运行时,它在我的书中说它应该允许你改变它的值s,即使它const.这是因为char*数据指向的const char*值与指向的值相同. *data在这种情况下不是const …

c++ const function member

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

将1d缓冲区重新组织为2d阵列

我给了一个int buffer[100]足够大小的1d缓冲区().我需要重新组织与2d数组相同的内存.我想要做的是将它转换为双指针(int ** p)然后将指针传递给将执行重组的alloc函数.函数返回后,我需要该语句p[2][2]才能工作.

这是代码:

#include <stdlib.h>
#include <stdio.h>

void alloc(int ** buf, int r, int c)
{
    int **temp;
    temp=buf;
    for(int i=0;i<r;i++)
        buf[i]=*(temp+i*c);
}
void main()
{
    int buffer[100];
    int **p=(int **)buffer;
    alloc(p, 4, 4);
    p[2][2]=10;
    printf("%d", p[2][2]);
}
Run Code Online (Sandbox Code Playgroud)

上面的代码编译成功,但是当我运行代码时,我在执行语句时得到访问冲突

p[2][2]=10;
Run Code Online (Sandbox Code Playgroud)

我无法理解问题所在.alloc函数有问题吗?或者是其他问题?

c c++ arrays pointers

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

错误"系统"是不明确的?

我有一个简单的程序,它工作正常,但system("CLS");system("pause");语句下面有红色的IntelliSense线.当我将光标移到它们上面时,它会说Error "system" is ambiguous.是什么原因造成的?

这是我的代码:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
  int choice = 0;
  const double PI = 3.14159;
  double sideSquare = 0.0;
  double radius = 0.0;
  double base = 0.0;
  double height = 0.0;

  cout << "This program calculates areas of 3 different objects." << endl;
  cout << "1.) Square" << endl;
  cout << "2.) Circle" << endl;
  cout << "3.) Right Triangle" << endl;
  cout << "4.) …
Run Code Online (Sandbox Code Playgroud)

c++ intellisense visual-studio-2010

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

运算符重载=和[]同时

我正在用c ++实现我自己的向量.

这是我的Vector类:

template <class T>
class Vector
{

    private :
        T *ptr;
        unsigned int numEle;

    public :
        T operator[] (unsigned int index)
        {
            if (index >= numEle)
                return ptr[0];
            else if (index < 0)
                 return ptr[0];
            else
                 return ptr[index];
        }

};
Run Code Online (Sandbox Code Playgroud)

我想做的是重载=操作符,这样当我写

  Vector v;
  v[2]=2; 
Run Code Online (Sandbox Code Playgroud)

它将值2分配给第二个索引....请帮助.. !!

c++ vector

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

我如何使用安全的strcpy_s函数?

我正在尝试strcpy更安全地更新呼叫strcpy_s.旧函数调用如下所示:

char buf[6];
strcpy(buf+1, "%#c");
Run Code Online (Sandbox Code Playgroud)

为了将上面的代码转换为更安全的版本,我会为size参数添加什么?

char buf[6];
strcpy_s(buf+1, /*SIZE GOES HERE*/, "%#c");
Run Code Online (Sandbox Code Playgroud)

我想我buf+1对代码部分感到困惑.这实际上只是一个指向寄存器ONE块的指针buf吗?如果是这样,我会为SIZE参数添加strcpy_s什么?我试过了:

  strcpy_s(buf+1, sizeof(buf+1), "%#c");
Run Code Online (Sandbox Code Playgroud)

这似乎有用,但我想知道是否有更好的方法来做到这一点.

c c++

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

将Dynamic Struct Array复制到另一个C++中

我的英语不是那么好,这就是为什么我的问题可能错了.但我有一个问题,我不知道如何解决它,或者甚至可能做到.

我定义了2个Structs:

typedef struct
{
  UINT16        ScriptNumber;
  std::string   ScriptText;
} StepStruct;


typedef struct
{
  std::string               SequenceName;
  std::string               DisplayName;
  StepStruct                SequenceSteps;
} SequenceStruct;
Run Code Online (Sandbox Code Playgroud)

如您所见,第一个Struct是第二个结构的成员.所以我希望两种结构都是动态的.所以我从Type中创建了2个动态数组,从Type中创建了StepStruct1个动态数组SequenceStruct.

Type StepStructs的两个动态数组定义如下:

StepStruct gsFirstSkript[] =
{    
  { 1 , "SkriptText One"},
  { 2 , "SkriptText Two"},
  { 45, "SkriptText Three"}
}

StepStruct gsSecondSkript[] =
{    
  { 48, "SkriptText One"},
  { 2 , "SkriptText Two"},
  { 45, "SkriptText Three"}
}
Run Code Online (Sandbox Code Playgroud)

那些结构属于类型StepStruct.现在我想对一个SequenceStructType 做同样的事情,但我想在Struct Member下分配我已经拥有的两个Arrays SequenceSteps.我的意思是:

SequenceStruct gsSequenceList[] =
{ …
Run Code Online (Sandbox Code Playgroud)

c++ arrays struct dynamic

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

我的for-loop没有启动

我知道有很多关于这个问题的主题,但这些都没有帮助我.我试图通过测试-10到10范围内的每个数字以及两个小数位来找到函数的根.我知道这可能不是最好的方式,但我是初学者,只是想尝试一下.不知怎的,循环不起作用,因为我总是得到-10作为输出.无论如何,这是我的代码:

 #include <iostream>
using namespace std;

double calc (double m,double n)
{
  double x;
  for (x=-10;x<10 && m*x+n==0; x+=0.01)
  {
   cout << x << endl;
  }
  return x;
}



int main()
{
  double m, n, x;

  cout << "......\n";
  cin >> m;                         // gradient
  cout << "........\n";
  cin >> n;                         // y-intercept
  x=calc(m,n);                      // using function to calculate
  cout << ".......... " << x<< endl; //output solution
  cout << "..............\n";        // Nothing of importance
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ for-loop

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