标签: const

如何在函数中返回const char*?

我给你们全部代码.这有点长,所以我最衷心地希望你得到它:

#include <iostream>
using namespace std;

char const* reverseWordsOnly(const char* s)
{
    int k;
    int p = strlen(s);


    for (int i = 0; i <= p; i++)
    {
        if (s[i] == ' ')
        {

            for (k = i - 1; (k != -1) && (s[k] != ' ') && (s[k] != ',') && (s[k] != ';') && (s[k] != '.'); k--)
                return &s[k];
            return " ";

        }
        if (s[i] == ',')
        {

            for (k = i - 1; (k != …
Run Code Online (Sandbox Code Playgroud)

c++ pointers const function

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

具有Const成员的构造函数的语法

我有一段时间没有做过C++编码,我的朋友在做作业时遇到了麻烦.我从来没有真正合作过const,而且这使得这成为一场噩梦,因为我无法弄清楚构造函数的正确语法.想象一下,我有这个dvd.h:

class DVD {
    const string title;
    int minutes;
    double price;

  public:
    DVD(const string t, int m, double p);
}
Run Code Online (Sandbox Code Playgroud)

3个私有成员变量,stringconst.构造函数也需要一个const string.

所以现在,dvd.cpp我可以做以下事情:

#include "dvd.h"

DVD::DVD(const string t, int m, double p) {
    const string title = t;
    minutes = m;
    price = p;
}
Run Code Online (Sandbox Code Playgroud)

一切都在世界上很好.然而,当我修改minutesdvd.hconst(这是他的教授是如何构造的文件),我们有这样的dvd.h:

class DVD {
    const string title;
    const int minutes; // Here is the change …
Run Code Online (Sandbox Code Playgroud)

c++ const

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

const int指针的行为 - C.

我有一个C程序

#include<stdio.h>

void f(const int* p)
{
  int j;
  p = &j;
  j = 10;
  printf("Inside function *p = %d\n",*p);
  *p = 5;
  printf("Inside function *p = %d\n",*p);
  j = 7;
  printf("Inside function *p = %d\n",*p);
}

int main()
{
  int i = 20, *q = &i;
  f(q);
}
Run Code Online (Sandbox Code Playgroud)

程序的编译给出了错误

分配只读位置*p

在线 *p = 5;

为什么分配j = 10;有效并且*p = 5;是错误的.

c pointers const

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

使用const char获取错误

我试图实现下面的函数,但foo()的输出是一堆废话.我试图运行调试器,并没有在append函数中看到任何问题.但是foo()中的总变量未正确赋值"abcdef".有什么想法吗?

int main()
{
    cout<<"foo is"<<endl;
    foo();
    return 0;
}

const char* append(const char* s1, const char* s2) {
    string s(s1);
    s += s2;
    return s.c_str();
}

void foo() {
    const char* total = append("abc", "def");
    cout<<total;
}
Run Code Online (Sandbox Code Playgroud)

c++ string const char

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

在c ++中使用delete时出现分段错误

我试图删除一个指针(对于char数组)时遇到seg错误.请帮我.我在这里做错了什么.请在下面找到代码段和输出.

代码:

# include <iostream>
using namespace std;
int main()
{
        int *p = new int;
        const char* c = new char[100];
        c = " hello";
        *p = 10;
        cout << "c= " << c << "*p = " << *p << endl;
        delete p;
        delete c;
        c = NULL;
        p = NULL;
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

c=  hello*p = 10
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

编辑:

如果我不使用new和删除字符数组,它是一个内存泄漏?我不能在我的代码中使用字符串,那么使用const char*variable的正确方法是什么?

提前致谢.

c++ linux pointers const char

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

函数的const参数是否自动通过引用传递?

如果我有一个如下所示的代码:

void foofunc(const int fooarg)
{
    // something here
}
Run Code Online (Sandbox Code Playgroud)

fooarg按值或按引用传递?至于fooargconst不是会得到修改,因此,如果按引用传递是确定的.


如果我有一个看起来像下面的代码:

void foofunc(int fooarg)
{
    // something here, but fooarg is not gonna get modified
}
Run Code Online (Sandbox Code Playgroud)

fooarg通过值或通过引用传递,也将是const或不是?情况与上述相同.它不会被修改.

c++ const pass-by-reference pass-by-value

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

能够在C中更改const的值,但不能在C++中更改

请考虑以下代码

#include <stdio.h>
#include <string.h>

main()
{
   const int a = 2;
   long p = (long)&a;
   int *c = (int *)p;
   *c =3;
   printf("%d", a);
}
Run Code Online (Sandbox Code Playgroud)

此代码可以将值更改为C中的a,但不能更改为C++中的值.我知道C++正在应用优化并替换awith的实例2.那么这是C++中的错误修复还是由于优化而偶然修复的错误?

c c++ optimization const

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

将对象变量传递为const ..&

我有一个看起来像的功能

bigvalue_t do_bigadd (const bigvalue_t& left, const bigvalue_t& right) {

}
Run Code Online (Sandbox Code Playgroud)

它在这里被称为

bigint bigint::operator+ (const bigint& that) const {
    bigint result;
    result.big_value = do_bigadd(this->big_value, that.big_value);
}
Run Code Online (Sandbox Code Playgroud)

我收到以下编译错误

错误:将'const bigint'作为'bigvalue_t bigint :: do_bigadd(const bigvalue_t&,const bigvalue_t&)的'this'参数传递'丢弃限定符[-fpermissive]

我知道出了什么问题,但我想不出办法解决它.如何将'this'作为const bigint&何时指针?

c++ const

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

如何初始化模板中的成员数组

我想有一个数组的长度取决于我的模板的参数,但我不断得到"预期的常量表达式"错误.

enum MyEnum
{
    FIRST,
    OTHER
};

template<MyEnum e> 
struct MyTemplate
{
    static const int arrSize;
    int myArr[arrSize];            // error C2057: expected constant expression
    // int myArr[e == FIRST ? 4 : 10]; // works, but isn't very readable...
};

template<>
const int MyTemplate<FIRST>::arrSize = 4;

template<>
const int MyTemplate<OTHER>::arrSize = 10;
Run Code Online (Sandbox Code Playgroud)

我必须使用的编译器不支持constexpr,或任何其他C++ 11功能,我也不能将数组大小作为模板参数传递.

编辑:我也一定不能用new.

谢谢

c++ templates allocation const

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

传递const引用:性能提升?

这之间是否有任何有价值的性能差异:

int myFunction(const int &a) { return 2 + a; }
Run Code Online (Sandbox Code Playgroud)

还有这个:

int myFunction(int a) { return 2 + a; }
Run Code Online (Sandbox Code Playgroud)

据我所知,第二种方法将变量的副本作为参数传递给新寄存器,这在asm代码中需要更多的指令.在这种情况下,这个函数被称为千次,性能至关重要,传递const引用而不是变量更好吗?

c++ performance const reference function

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