在命名空间中包装header和cpp文件内容,或者只包装头内容然后在cpp文件中使用命名空间之间有什么区别吗?
差异我的意思是任何排序性能损失或稍微不同的语义,可能会导致问题或我需要注意的任何事情.
例:
// header
namespace X
{
class Foo
{
public:
void TheFunc();
};
}
// cpp
namespace X
{
void Foo::TheFunc()
{
return;
}
}
Run Code Online (Sandbox Code Playgroud)
VS
// header
namespace X
{
class Foo
{
public:
void TheFunc();
};
}
// cpp
using namespace X;
{
void Foo::TheFunc()
{
return;
}
}
Run Code Online (Sandbox Code Playgroud)
如果没有差异,首选形式是什么?为什么?
git update-index --assume-unchanged <filename>
回报fatal: Unable to mark file <filename>
.
我需要做些什么来解决这个问题?抱怨什么,为什么?
我对Java比较陌生,我已经开始研究一个项目了.但是,我遇到了一个障碍.我需要一种方法在一天中的某些时间运行.我已经做了很多搜索,但我找不到任何看起来像它会做的伎俩.我遇到了Timer类,但它似乎以一定的间隔运行.Scheduler类似乎有同样的问题.我也遇到了Quartz,但我认为我需要更轻量级的东西,我只能看到如何间歇地做事.
也许,仅仅因为我是新人,我错过了一些可以帮助我完成这些课程的东西,但我真的很困难,可以使用一些帮助.
如果有人能指出我会在一天中的某个特定时间运行某个课程(每天都能获得奖励积分),并告诉我如何正确使用该课程,这将是非常棒的!
TL; DR:需要一个在一天中的某个时间做某事的课程,而不是一个时间间隔,因为该课程可能会在一天中多次重启.
我正在循环ArrayList<Bar>
.Foo
延伸Bar
.我正在尝试Foo
在列表中找到一个实例.
ArrayList<Bar> list = new ArrayList<Bar>();
// fill list with items that are instances of Foo and Bar
for (int i = 0; i < list.size(); i++)
{
Bar bar = list.get(i);
if (bar.getClass().getName().equals("com.me.Foo")) // "if(bar instanceof Foo)" never passes
{
System.out.println("bar is an instance of Foo");
Foo foo = (Foo) bar;
}
}
Run Code Online (Sandbox Code Playgroud)
如果此部分代码运行,我会收到此错误:
java.lang.ClassCastException: com.me.Foo cannot be cast to com.me.Foo
// stack
Run Code Online (Sandbox Code Playgroud)
但是,在另一个地方,我有一个方法返回一个实例,Bar
我可以检查它是否是一个Foo
使用的实例instanceof
然后Bar …
这是错误:
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ios:39,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ostream:40,
from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iostream:40,
from date.h:15,
from date.cpp:13:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/ios_base.h:790: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:47: error: within this context
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd: In copy constructor ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/iosfwd:56: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here
date.cpp: In function ‘std::ostream operator<<(std::ostream&, Date&)’:
date.cpp:389: note: synthesized method ‘std::basic_ostream<char, std::char_traits<char> >::basic_ostream(const std::basic_ostream<char, std::char_traits<char> >&)’ first required here
make: *** [date.o] …
Run Code Online (Sandbox Code Playgroud) 假设我有一个具有两个内联函数的类:
class Class {
public:
void numberFunc();
int getNumber() { return number; }
private:
int number;
};
inline void Class::numberFunc()
{
number = 1937;
}
Run Code Online (Sandbox Code Playgroud)
我实例化该类,并调用该类中的两个函数:
int main() {
Class cls;
cls.numberFunc();
cout << cls.getNumber() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道内联函数仍然是类的成员,但我也理解,内联函数体内的代码只是插入它被调用的位置.看来,作为插入的结果,我不能直接访问成员变量number
,因为据我所知,main()
编译器中的代码如下所示:
main() {
Class cls;
cls.number = 1937;
cout << cls.number << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人可以向我解释为什么我仍然可以访问这些私人成员,或者纠正我对内联函数的理解?我知道编译器可以选择忽略inline
某些函数; 那是怎么回事?
输出:
1937年
为了解释数组只是我们班级的指针(用C++),我的教授向我们展示了这个:
array[5] // cout'ing this
*(array + 5) // would return the same value as this
Run Code Online (Sandbox Code Playgroud)
我完全理解它有点麻烦.这是我的想法:
array
是第一个位置的地址,所以如果我们向该地址添加5,我们在内存中移动5个地址.指针运算符从内存位置提取数据.
这是正确的想法吗?这个想法对我来说仍然感到模糊,只是觉得我完全不明白.我想听到别人解释它可能会帮助我更多地理解它.提前致谢!
我正在制作国际象棋游戏,我希望有一系列作品.
如果我是对的,在Java中你可以拥有一个抽象Piece
类并拥有King
或Queen
扩展该类.如果我做的阵列Piece
的I可以放置一个King
数组中的一块地方和Queen
在另一个地方,因为这两个部分King
和Queen
延伸Piece
.
有没有办法用C++中的结构做到这一点?
我有一个vector
指向抽象类型的指针Rock
:
vector<Rock*> rocks;
Run Code Online (Sandbox Code Playgroud)
如果我使用迭代器遍历向量然后尝试通过迭代器访问对象(扩展的非抽象类Rock
),我在XCode 4中得到一个'EXC_BAD_ACCESS'错误:
vector<Rock*>::iterator rockIter;
for (rockIter = rocks.begin(); rockIter != rocks.end(); ++rockIter)
{
bullet.hit(*(*rockIter));
}
Run Code Online (Sandbox Code Playgroud)
但循环通过它通常是没有问题:
for (int i = 0; i < rocks.size(); i++)
{
bullet.hit(*rocks[i]);
}
Run Code Online (Sandbox Code Playgroud)
该功能hit()
如下:
bool Bullet::hit(Rock & rock)
Run Code Online (Sandbox Code Playgroud)
我想到了,*(*rockIter)
并*rock[i]
会做同样的事情但显然他们不这样做.有什么不同,我怎么能通过像我一样的迭代器传递向量中的对象的引用*rock[i]
?
我正在查看游戏中的一些代码,我遇到了一些我以前没见过的东西,我真的不知道最近发生了什么.
public abstract class Entity
{
public Entity(World world)
{
// irrelevent code
entityInit();
}
protected abstract void entityInit();
}
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?它呼吁时会发生什么entityInit()
?