最近,我一直在开发一种在我的代码中做很多事情的做法const:
(1)功能论证,我知道永远不会改变.例如:
void foo (const int i, const string s)
^^^^^ ^^^^^
Run Code Online (Sandbox Code Playgroud)
(2)返回类型为const.例如:
struct A {
...
const int foo () { return ...; }
^^^^^
operator const bool () const { return ...; }
^^^^^
};
Run Code Online (Sandbox Code Playgroud)
(3)整数或字符串的平凡计算.例如:
const uint size = vec.size();
^^^^^
const string s2 = s1 + "hello ";
^^^^^
Run Code Online (Sandbox Code Playgroud)
......还有更多的地方.通常在其他现实世界的代码中,我没有看到标记为的这种小规模变量const.但我想,让它们const永远不会受到伤害.这是一个很好的编程习惯吗?
我可以把T和一个包裹T在union并检查他们,因为我喜欢?
union Example {
T value;
struct Wrapped {
T wrapped;
} wrapper;
};
Run Code Online (Sandbox Code Playgroud)
// for simplicity T = int
Example ex;
ex.value = 12;
cout << ex.wrapper.wrapped; // ?
Run Code Online (Sandbox Code Playgroud)
C++ 11标准只保证保存对常见初始序列的检查,但value不是struct.我猜答案是否定的,因为包装类型甚至不能保证与其解包对应的内存兼容,并且访问非活动成员只能在常见的初始序列上明确定义.
如果我没有定义我自己的构造函数,那么Base *b = new Base;vs 之间有什么区别Base *b = new Base();吗?
我想使用fwrite将对象写入顺序文件.班级就像
class A{
int a;
int b;
public:
//interface
}
Run Code Online (Sandbox Code Playgroud)
当我将一个对象写入文件时 我在游荡,我可以fwrite( this, sizeof(int), 2, fo)用来编写前两个整数.
问题是:即使this在对象的最开头存在虚拟表,也保证指向对象数据的开始.所以上面的操作是安全的.
即使通过普通的按值调用参数传递机制将对象传递给函数,理论上它们可以保护和隔离调用参数,但是仍然可能发生可能影响甚至损坏的副作用,用作参数的对象.例如,如果用作参数的对象分配内存并在销毁时释放该内存,那么在调用析构函数时,函数内部的本地副本将释放相同的内存.这将使原始物体损坏并且实际上无用.
这是用C++编写的:完整参考
在这个程序在这里
#include<iostream>
using namespace std;
class Sample
{
public:
int *ptr;
Sample(int i)
{
ptr = new int(i);
}
~Sample()
{
cout<<"destroyed";
delete ptr;
}
void PrintVal()
{
cout << "The value is " << *ptr;
}
};
void SomeFunc(Sample x)
{
cout << "Say i am in someFunc " << endl;
}
int main()
{
Sample s1= 10;
SomeFunc(s1);
s1.PrintVal();
}
Run Code Online (Sandbox Code Playgroud)
它会在对象s1从对象返回时被销毁时生成运行时错误.我无法弄清楚为什么会发生这种情况,因为应该制作副本.我想也许是因为班级定义中没有复制构造函数.但我很惊讶地发现,如果使用这个函数声明
void SomeFunc(Sample &x)
{
cout << "Say i am in someFunc " << endl;
} …Run Code Online (Sandbox Code Playgroud) C++是否在简单的POD typedef 上进行值初始化?
假设
typedef T* Ptr;
Run Code Online (Sandbox Code Playgroud)
不
Ptr()
Run Code Online (Sandbox Code Playgroud)
做值初始化并保证相等(T*)0?
例如
Ptr p = Ptr();
return Ptr();
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习如何使用常量函数和对象,但是,我有一些错误使我保持了一个多小时,我似乎无法弄明白.我跟着一个简单的例子,我想我在路上的某个地方迷路了.这是我的代码.
Main.cpp的
#include <iostream>
#include "ExampleClass.h"
int main(){
ExampleClass exampleObj; // object used to call members of ExampleClass.
exampleObj.printText(); // calls printVar from the ExampleClass.
const ExampleClass constantObject; // object used to call constant members of ExampleClass.
constantObject.printConstText(); // calls printConstVar from the ExampleClass.
return 0;
}
Run Code Online (Sandbox Code Playgroud)
ExampleClass.h
#ifndef EXAMPLECLASS_H
#define EXAMPLECLASS_H
class ExampleClass
{
public:
void printText();
void printConstText() const;
};
#endif // EXAMPLECLASS_H
Run Code Online (Sandbox Code Playgroud)
ExampleClass.cpp
#include <iostream>
#include "ExampleClass.h"
void ExampleClass::printText(){
std::cout << "The code works!" << "\n";
} …Run Code Online (Sandbox Code Playgroud) C++中的引用类型也是POD类型吗?是int&POD类型吗?那怎么样?
struct Q { int& i; }
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?
我有以下课程:
#include <iostream>
#include <string>
using namespace std;
class CLS
{
int value;
string str_value;
public:
CLS(int param) { value = param; }
CLS(string param)
{
str_value = param;
}
};
int main()
{
CLS a(2);
CLS b = 3;
CLS c("4");
CLS d = "5"; // Error: invalid conversion from 'const char*' to 'int'
}
Run Code Online (Sandbox Code Playgroud)
我搜索了没有运气的错误原因.
用字符串文字构造是否正确?如果不是,为什么?如果是的话,我的代码出了什么问题?
我使用gcc 5.3与Code :: Blocks 16.1.