constexpr Student(const char* name, int age) : name_(name), age_(age) {} void print() const { cout << name_ << ' ' << age_ << '\\n'; } private: const char* name_; int age_; }; void test() { constexpr Student s{"zhangsan", 18}; s.print(); } 总结 const可以修饰...
constexpr myType(const char *name,int age):name(name),age(age){}; constexpr const char * getname(){ return name; } constexpr int getage(){ return age; } private: const char* name; int age; //其它结构体成员 }; int main() { constexpr struct myType mt { "zhangsan", 10 }; ...
constexprmyType(constchar*name,intage):name(name),age(age){}; constexprconstchar*getname(){ returnname; } constexprintgetage(){ returnage; } private: constchar*name; intage; //其它结构体成员 }; intmain() { constexprstructmyType mt{"zhangsan",10}; constexprconstchar*name=mt.getname...
class Student { public: constexpr Student(const char* name, int age) : name_(name), age_(age) {} void print() const { cout << name_ << ' ' << age_ << '\n'; } private: const char* name_; int age_; }; void test() { constexpr Student s{"zhangsan", ...
1、const char *p 表示指针p指向的内容不能改变 2、char * const p,就是将p声明为常指针,它的地址不能改变。 const char* p0 = "aaaa"; const char* p1 = "abcd";// 表示指针p指向的内容不能改变 但是p指向的位置是可以变得 p1 = p0;
classStudent{public:constexprStudent(constchar*name,intage):name_(name),age_(age){}voidprint()const{cout<<name_<<' '<<age_<<'\n';}private:constchar*name_;intage_;};voidtest(){constexprStudent s{"zhangsan",18};s.print();}
class Student { public: constexpr Student(const char* name, int age) : name_(name), age_(age) {} void print() const { cout << name_ << ' ' << age_ << '\n'; } private: const char* name_; int age_; }; void test() { constexpr Student s{"zhangsan", 18}; s.print(...
constchar*name_; intage_; }; voidtest(){ constexprStudent s{"zhangsan",18}; s.print(); } 总结 •const可以修饰编译期和运行期的常量,而constexpr只能修饰编译期的常量 •const在仍然可以通过const_cast类型转换来修改值,而constexpr是不可以修改的,其实可以将const理解为只读变量更符合其含义 ...
classStudent{public:constexprStudent(constchar*name,intage):name_(name),age_(age){}voidprint()const{cout<<name_<<' '<<age_<<'\n';}private:constchar*name_;intage_;};voidtest(){constexprStudent s{"zhangsan",18};s.print();}
const char* name_; int age_; }; void test() { constexpr Student s{"zhangsan", 18}; s.print(); } 总结 const可以修饰编译期和运行期的常量,而constexpr只能修饰编译期的常量 const在仍然可以通过const_cast类型转换来修改值,而constexpr是不可以修改的,其实可以将const理解为只读变量更符合其含义 ...