我一直在抓挠这个问题.我怀疑我;我只是在这里愚蠢,但我似乎无法获得const或静态成员进行初始化,所以我可以在整个课程中使用它.
这是一个展示我的问题的例子(或者更确切地说是我的误解):
using System;
namespace ConstExample
{
public class HasImmutableMember
{
// static private double fSectionLengthTolerancePerInch = 1 / (20 * 12); // tolerance is 1" per every 20'
private const double fSectionLengthTolerancePerInch = 1 / (20 * 12); // tolerance is 1" per every 20'
static HasImmutableMember()
{
Console.WriteLine("static c'tor: " + fSectionLengthTolerancePerInch);
}
public HasImmutableMember()
{
Console.WriteLine("instance c'tor: " + fSectionLengthTolerancePerInch);
}
}
public class Program
{
public void Main(string[] args)
{
HasImmutableMember instance = new HasImmutableMember();
} …Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
#include<stdlib.h>
int *func(int *);
int main(void)
{
int i,size;
const int *arr=func(&size);
for(i=0;i<size;i++)
{
printf("Enter a[%d] : ",i);
scanf("%d",&arr[i]);
}
for(i=0;i<size;i++)
{
printf("%d\t",arr[i]);
}
return 0;
}
int *func(int *psize)
{
int *p;
printf("Enter the size: ");
scanf("%d",psize);
p=(int *)malloc(*psize *sizeof(int));
return p;
}
Run Code Online (Sandbox Code Playgroud)
输入大小:3
输入[0]:1
输入[1]:2
输入[2]:3
1 2 3
看看这段代码:
int main() {
int foo = 0;
const int *ptr = &foo;
*ptr = 1; // <-- Error here
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译时,clang给了我一个错误:
const.c:5:7: error: read-only variable is not assignable
Run Code Online (Sandbox Code Playgroud)
是的,ptr是const,但foo不是.为什么我不能分配值foo?
查看C标准库中的大多数函数,似乎缺少const,其中指定so,通常是首选.
例如:
ctype.h/c
extern int isupper(int __c) __ATTR_CONST__;
extern int islower(int __c) __ATTR_CONST__;
extern int isdigit(int __c) __ATTR_CONST__;
Run Code Online (Sandbox Code Playgroud)
为什么不是这些:
extern int isupper(const int __c) __ATTR_CONST__;
extern int islower(const int __c) __ATTR_CONST__;
extern int isdigit(const int __c) __ATTR_CONST__;
Run Code Online (Sandbox Code Playgroud)
他们毕竟只观察参数:
int isupper(int c) {
return _pctype[c] & _UPPER;
}
int islower(int c) {
return _pctype[c] & _LOWER;
}
int isdigit(int c) {
return _pctype[c] & _DIGIT;
}
Run Code Online (Sandbox Code Playgroud)
或者让我们在string.c中使用一个函数:
void *memcpy(void *dest, const void *src, size_t count)
{
char *tmp = …Run Code Online (Sandbox Code Playgroud) 尝试编译时,我在Eclipse中遇到以下错误(c ++)
../CardDeck.cpp:17:22:错误:将'const CardDeck'作为'int CardDeck :: size()'的'this'参数传递,丢弃限定符[-fpermissive]
如果我将int size()方法更改为int size()const,则错误消息将消失并且已编译.我不知道为什么?
该.H文件如下:
#include "Card.h"
#include <vector>
using namespace std;
class CardDeck{
private:
vector<Card*> deck;
public:
int size();
CardDeck();
CardDeck(const CardDeck& rhs);
CardDeck& operator=(const CardDeck& rhs);
Card& draw();
Card& top();
bool isEmpty();
void clear();
int value();
CardDeck& operator+=(const CardDeck& rhs); /// not sure if to return ref
CardDeck& operator+(const CardDeck& rhs);
friend CardDeck& operator*(unsigned int num,CardDeck& rhs);
friend CardDeck& operator*(CardDeck& lhs,unsigned int num);
bool operator<=(const CardDeck& …Run Code Online (Sandbox Code Playgroud) 我见过很多人将他们的函数参数设置为:
function(const myType &myObj)
Run Code Online (Sandbox Code Playgroud)
我不明白为什么他们使用&后的类型?
似乎const足以阻止构造函数被调用.
所以,我写了下面的代码,我发现结果没有优势.有人可以解释一下吗?
#include <iostream>
using namespace std;
class myclass
{
public:
myclass()
{
cout<<"constructor is called\n";
}
int field;
};
void func1(myclass a)
{
cout<<"func1: "<<a.field<<"\n";
}
void func2(const myclass a)
{
cout<<"func2: "<<a.field<<"\n";
}
void func3(const myclass &a)
{
cout<<"func3: "<<a.field<<"\n";
}
int main ()
{
myclass obj;
obj.field=3;
cout<<"----------------\n";
func1(obj);
cout<<"----------------\n";
func2(obj);
cout<<"----------------\n";
func3(obj);
cout<<"----------------\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果:
constructor is called
----------------
func1: 3
----------------
func2: 3
---------------- …Run Code Online (Sandbox Code Playgroud) 我需要为我的类创建一个复制构造函数,Immagine如下所示:
Immagine::Immagine(Immagine& i)
{
...
}
Run Code Online (Sandbox Code Playgroud)
很明显,当我试着打电话时,我有类似的东西:
error: invalid initialization of non-const reference of type ‘Immagine&’ from an rvalue of type ‘Immagine’因为我会声明它:
Immagine::Immagine(const Immagine& i)
{
...
}
Run Code Online (Sandbox Code Playgroud)
但我无法做到这一点,因为,为了对一个Immagine成员对象进行初始化,我使用了一个函数
Immagine::Immagine(const Immagine& i)
{
dlib::array2d<dlib::rgb_pixel>& r=i.v; //v is a member of type dlib::array2d<dlib::rgb_pixel>
dlib::assign_image(this->dlib_immagine,r);
}
Run Code Online (Sandbox Code Playgroud)
功能dlib::assign_image(dst,src)是封装功能,并复制src到dst,但没有被声明const为src参数,所以如果我声明i作为const我得到一个错误......我该如何解决这个问题?
我查看了所有其他类似主题的帖子,没有任何帮助,所以请不要标记为重复.
我正在定义main()一个const int SIZE = 20;.然后,我将此作为参数传递给我的函数Mode:
int* Mode(int* numbers, int & mode, const int SIZE)
{
int occurences[SIZE];
// Calcualte mode
}
Run Code Online (Sandbox Code Playgroud)
但是,我得到了错误expression must have a constant value.
我的函数调用(在main中)看起来像这样:
int* occurencesPtr = Mode(numbersPtr, mode, SIZE);
Run Code Online (Sandbox Code Playgroud)
有了SIZE开头的文字被定义20.
我理解错误是因为函数的版本SIZE仅在调用函数时获取其值(?),但我不知道如何解决这个问题.
我甚至尝试过传递给函数a const int * const SIZEPtr = &SIZE,但这也没有用.救命?
编辑:我不是想使用可变大小!! 请注意,我做了SIZE一个const无处不在!我只想使用相同的SIZE常量来声明我的数组.
编辑:动态数组不是我需要的.我只想要一个普通的,命名的数组,用传递给函数的常量大小值定义.
我想实现一个快速随机生成器,我遇到了这个网站:https://en.wikipedia.org/wiki/Xorshift,其中提出以下代码
#include <stdint.h>
/* The state must be seeded so that it is not everywhere zero. */
uint64_t s[2];
uint64_t xorshift128plus(void) {
uint64_t x = s[0];
uint64_t const y = s[1];
s[0] = y;
x ^= x << 23; // a
s[1] = x ^ y ^ (x >> 17) ^ (y >> 26); // b, c
return s[1] + y;
}
Run Code Online (Sandbox Code Playgroud)
我想知道这里的const是否有用,我可以安全地删除吗?
我认为枚举是静态的,有什么意义const enum呢?
例如:
const typedef enum
{
NORMAL_FUN = 1,
GREAT_FUN = 2,
TERRIBLE_FUN = 3,
} Annoying;
Run Code Online (Sandbox Code Playgroud)
我有一个旧的程序,我被迫与之合作(来自设备制造商),并且我一直在讨论定义的枚举const typedef enum.
现在,我习惯了C#,所以我并不完全理解所有正在进行的C++技巧,但这种情况似乎很简单.
从程序的编码看,似乎所有类型的变量Annoying都要随时随地改变.
它们并不意味着不变.长话短说,编译器不喜欢它.
这个样本是在2010年之前的某个时候写回来的,所以这可能是某种版本的差异,但是const typedef enum甚至意味着什么?