有什么区别:
char * const
Run Code Online (Sandbox Code Playgroud)
和
const char *
Run Code Online (Sandbox Code Playgroud) 我想知道它们之间的区别
const int* ptr;
Run Code Online (Sandbox Code Playgroud)
和
int * const ptr;
Run Code Online (Sandbox Code Playgroud)
以及它是如何工作的.
我很难理解或记住这一点.请帮忙.
我想声明一个常量char数组的常量数组.如果我这样写:
const char foo[] = "Foo";
const char bar[] = "Bar";
const char* const foobar[2] = { foo, bar };
Run Code Online (Sandbox Code Playgroud)
它似乎工作,但如果我尝试使用"螺旋规则"读取它,foobar读作:
"foobar是一个常量(??)的数组2到指向char常量的指针"
使用这个堆栈溢出答案,
const适用于它剩下的东西.如果左边没有任何内容,则它适用于它的权利.
第一个const将应用于char,第二个const也适用于相同的char.
两种阅读方式都没有意义,但代码确实有效(至少在Arduino [抱歉]).哪个const是常数?是否有更合理的方式来编写它?
template <typename T>
void f(T t)
{}
int x = 1;
const int & rx = x;
const int * px = &x;
f(rx); // t is int
f(px); // t is const int *, instead of int *, WHY???
Run Code Online (Sandbox Code Playgroud)
我现在很困惑。根据Effective Modern c++,
重要的是要认识到 const 仅被按值参数忽略。正如我们所见,对于const 引用或const 指针的参数,在类型推导过程中保留expr 的常量性。
我认为这意味着
template <typename T>
void f(T * t)
{}
f(px); // t is const int *
template <typename T>
void f(T & t)
{}
f(cx); // t is const …Run Code Online (Sandbox Code Playgroud) c++ templates pointers reference template-argument-deduction
我一直在阅读关于此的不同内容,这看起来很危险.有人可以告诉我正确的模式来定义可以全局使用的通知字符串吗?我尝试过的所有内容都会导致链接器错误.例如,在我的GlobalVariables单例中,我添加了:
#import <Foundation/Foundation.h>
extern NSString *kMPTimeChanged;
@interface GlobalVariables : NSObject etc.
Run Code Online (Sandbox Code Playgroud)
然后在init中:
@implementation GlobalVariables
#pragma mark Singleton Methods
+ (id)sharedGlobals {
static GlobalVariables *sharedGlobals = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedGlobals = [[self alloc] init];
});
return sharedGlobals;
}
- (id)init {
if (self = [super init]) {
kMPTimeChanged=@"kMPTimeChanged";
return self;
}
Run Code Online (Sandbox Code Playgroud)
它没有构建,我有多个错误.
我可以知道为什么这个任务是可能的吗?
char const *c = "Hello";
c = "there!";
Run Code Online (Sandbox Code Playgroud)
是不是指向一个不能修改内容的位置。据我所知,它正在创建另一个对象并使 c 指向它。是真的吗。
对此的任何其他亮点将不胜感激。
码:
const char * const key;
Run Code Online (Sandbox Code Playgroud)
上面的指针有2个const,我第一次看到这样的东西.
我知道第一个const使指针指向的值不可变,但第二个const是否使指针本身不可变?
有人可以帮忙解释一下吗?
@Update:
我写了一个程序,证明答案是正确的.
#include <stdio.h>
void testNoConstPoiner() {
int i = 10;
int *pi = &i;
(*pi)++;
printf("%d\n", i);
}
void testPreConstPoinerChangePointedValue() {
int i = 10;
const int *pi = &i;
// this line will compile error
// (*pi)++;
printf("%d\n", *pi);
}
void testPreConstPoinerChangePointer() {
int i = 10;
int j = 20;
const int *pi = &i;
pi = &j;
printf("%d\n", *pi);
}
void testAfterConstPoinerChangePointedValue() {
int i = 10; …Run Code Online (Sandbox Code Playgroud) 对于给定的类型T:
const T *和之间有什么区别T * const?
还有,还有其他地方const可以去吗?比如,是T const *一件事吗?你能const在一个表达中有多个,比如const T * const?
假设我有一个指向整数的指针数组(即每个元素都是指向int的指针)
int** ptrArray;
Run Code Online (Sandbox Code Playgroud)
我想阻止更改数组条目指向的整数
我需要把const放在哪里?
1. const int ** ptrArray
2. int const ** ptrArray
3. int *const* ptrArray
4. int ** const ptrArray
Run Code Online (Sandbox Code Playgroud)
对此有什么规定吗?像"第一个const保护数据","第二个const保护指针"等等?
const的位置背后是否有任何逻辑?放置的地方与保护的地方之间有什么联系?
这对我来说是一个令人困惑的问题,如果有人可以给我任何指导或链接,我会真的很高兴我可以阅读更多关于如何以及在何处使用const基于我想要保护的内容(如果我需要使用三维数组中的const或左右)
我正在尝试了解在测试更改时我所看到的内容.该平台是带有GCC 4.8的openSUSE 42,但它可能会影响其他人.测试代码和错误如下.
$ cat test.cxx
#include <string>
#if (__cplusplus >= 201103L)
# define STATIC_CONSTEXPR static constexpr
# define CONSTEXPR constexpr
#else
# define STATIC_CONSTEXPR static const
# define CONSTEXPR
#endif
struct Name
{
STATIC_CONSTEXPR char* GetName() {return "XXX";}
};
int main(int argc, char* arv[])
{
const char* name = Name::GetName();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
和:
$ g++ -O3 -std=c++11 test.cxx -o test.exe
test.cxx: In static member function ‘static constexpr char* Name::GetName()’:
test.cxx:13:44: warning: deprecated conversion from string constant to ‘char*’ …Run Code Online (Sandbox Code Playgroud) c ×6
c++ ×6
const ×6
pointers ×5
arrays ×1
c++11 ×1
cocoa-touch ×1
constexpr ×1
ios ×1
objective-c ×1
reference ×1
template-argument-deduction ×1
templates ×1