当您从Xcode创建一个嵌入CoreData的新应用程序时,您可以在委托的实现文件中获得这些行:
@synthesize window=_window;
@synthesize managedObjectContext=__managedObjectContext;
Run Code Online (Sandbox Code Playgroud)
仅使用下划线或双倍之间有什么区别?只写作有什么区别:
@synthesize window;
Run Code Online (Sandbox Code Playgroud) 关于以下"模式"的最佳实践是什么?
#ifndef BLAFOO_H
#define BLAFOO_H
/* ...
* ...
*/
#endif /* BLAFOO_H */
Run Code Online (Sandbox Code Playgroud)
我应该如何命名#define指令中的标题?我已经看到了从说BLAFOO_H要__BLAFOO_H来_BLAFOO_H_等.
在C++中,是否可以用数字启动类名?例如,
template <class T> class 2DArray {
public:
// 1D ARRAY CLASS
class 1DArray {
public:
1DArray() { Create(); }
1DArray(iterator arr) : array1d_(arr) { }
explicit 1DArray(size_type cols, const T& t = T()) { Create(cols, t); }
1DArray(const 1DArray& arr) { Create(arr.begin(), arr.end()); }
1DArray& operator=(const 2DArray&);
~1DArray() { Uncreate(); }
T& operator[](size_type n) {
return array1d_[n];
}
const T& operator[](size_type n) const {
return array1d_[n];
}
}
Run Code Online (Sandbox Code Playgroud) 在C++中,可以创建一个返回对私有字段的引用的访问器.
class Cls {
private:
int _attr;
public:
int& attr() { return _attr; }
};
Run Code Online (Sandbox Code Playgroud)
这样可以如下访问该属性:
// set
c.attr() = 4;
// get
cout << c.attr() << endl;
Run Code Online (Sandbox Code Playgroud)
这种风格的访问者惯用/良好实践吗?一个普通的C++程序员会看到这种访问者的风格会感到惊讶吗?(提示:我第一次看到这个时很惊讶,但有点喜欢这种风格)
我正在读K&R书.我读:
...仅供标准库函数使用的名称,
_因此它们不太可能与用户程序中的名称冲突...
这究竟意味着什么,请解释真实简单实用的方法.
我的理解是:
如果我想使用math.h中定义的sqrt那么
#include <math.h>
#define sqrt(x) x*x*x
main()
{
int x=4;
_sqrt(x); // That is from the header file math.h
sqrt(x); // my own defined macro
/*or its the reverse way _sqrt for my own defined macro so it won't collide with original sqrt i.e. without _ for sqrt from math.h */
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在,我使用了在stackoverflow上读取代码__.sys/syscall.h在Windows中不存在,所以我们必须使用
#if __linux
#include <sys/syscall.h>
#elif defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#endif
Run Code Online (Sandbox Code Playgroud)
确切__使用的地方和b/w __&的区别_ …
我有一些c ++代码,有一堆#ifdef WIN32,否则我们假设它的IOS代码.但是我现在正试图将这个相同的c ++代码用于android端口.
#ifdef WIN32 ||是否有某种等价物 ANDROID?
我在整个申请中使用std::mutex并std::lock_guard以适当的RAII方式:
struct Foo {
int f() const
{
std::lock_guard<std::mutex> locker(m_mutex);
return m_i;
}
private:
int m_i = 0;
mutable std::mutex m_mutex;
};
Run Code Online (Sandbox Code Playgroud)
它总是有效,但我刚刚为另一个类增加了并行性,并且在这个新类中locker抛出std::system_error.问题出在这里(xthread标题):
inline int _Mtx_lockX(_Mtx_t *_Mtx)
{ // throw exception on failure
return (_Check_C_return(_Mtx_lock(_Mtx)));
}
Run Code Online (Sandbox Code Playgroud)
_Mtx_lock 当预期值为0时返回3.不知道3意味着什么.
VS2013,v120_x64运行时.
如果我#include <vector.h>输入我的源文件,我会收到此警告:
make -f Makefile CFG=Debug
g++ -c -g -o "Debug/mynn.o" "mynn.cpp"
In file included from C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/vector.h:59,
from mynn.h:7,
from mynn.cpp:1:
**C:/MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <iostream> instead of the deprecated header <iostream.h>. To disable this warning use -Wno-deprecated.**
g++ …Run Code Online (Sandbox Code Playgroud) 我想知道包装C++ STL容器以保持一致性并且能够在不修改客户端代码的情况下交换实现是一个好主意.
例如,在一个项目中,我们使用CamelCase命名类和成员函数(Foo :: DoSomething()),我会将std :: list包装成这样的类:
template<typename T>
class List
{
public:
typedef std::list<T>::iterator Iterator;
typedef std::list<T>::const_iterator ConstIterator;
// more typedefs for other types.
List() {}
List(const List& rhs) : _list(rhs._list) {}
List& operator=(const List& rhs)
{
_list = rhs._list;
}
T& Front()
{
return _list.front();
}
const T& Front() const
{
return _list.front();
}
void PushFront(const T& x)
{
_list.push_front(x);
}
void PopFront()
{
_list.pop_front();
}
// replace all other member function of std::list.
private:
std::list<T> _list;
}; …Run Code Online (Sandbox Code Playgroud) 我试图检查是否#include <file.h>已经使用C++宏声明了.
在file.h我做:
#ifndef FILE.H
#define FILE.H
class A
{
//do routines
};
#endif
Run Code Online (Sandbox Code Playgroud)
在第二个文件中,second.h我想检查是否file.h已经包含.
典型的伪代码:
#ifndef "file.h"
#include "file.h"
#endif
#ifndef SECOND.H
#define SECOND.H
class second
{
//do routine
};
#endif
Run Code Online (Sandbox Code Playgroud)
我尝试了一些#ifndef指令,但没有快乐.有谁知道如何实现这一目标?