c++ c++-faq copy-constructor assignment-operator rule-of-three
我知道C中的全局变量有时会有extern关键字.什么是extern变量?宣言是什么样的?它的范围是什么?
这与跨源文件共享变量有关,但这是如何工作的?我在哪里用extern?
在:http://www.learncpp.com/cpp-tutorial/19-header-files/
提到以下内容:
add.cpp:
int add(int x, int y)
{
return x + y;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <iostream>
int add(int x, int y); // forward declaration using function prototype
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is " << add(3, 4) << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我们使用了前向声明,以便编译器在编译时知道"
add"是什么main.cpp.如前所述,为要在其他文件中使用的每个函数编写前向声明可能会很快变得乏味.
你能进一步解释" 前瞻性宣言 "吗?如果我们在main()函数中使用它会有什么问题?
我是一名试图学习C++的JAVA开发人员,但我真的不知道标准函数声明的最佳实践是什么.
在课堂里:
class Clazz
{
public:
void Fun1()
{
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
或者在外面:
class Clazz
{
public:
void Fun1();
}
Clazz::Fun1(){
// Do something
}
Run Code Online (Sandbox Code Playgroud)
我觉得第二个可读性较差......
什么时候应该使用for循环而不是while循环?
我认为以下循环是相同的,除了它们的语法.如果是这样,为什么选择一个而不是另一个呢?
int i;
for (i = 0; i < arr.length; i++) {
// do work
}
int i = 0;
while (i < arr.length) {
// do work
i++;
}
Run Code Online (Sandbox Code Playgroud) 在阅读问题后,我知道声明和定义之间的区别.那么这意味着定义等于声明加初始化吗?
我想强制一个小函数不被编译为内联函数,即使它非常简单.我认为这对于调试目的很有用.有没有关键字可以做到这一点?
我有一个头文件xh,它包含多个*.c源文件.此头文件定义了一些结构变量.
我在头文件的开头添加了多个包含防护措施:
#ifndef X_H
#define X_H
...
..
//header file declarations and definitons.
#endif//X_H
Run Code Online (Sandbox Code Playgroud)
在构建时,我得到与多个定义相关的链接器错误.我理解这个问题.
不会像我一样在头文件的顶部有多重包含防护,防止头文件xh的多个包含,从而避免xh中存在的变量的多个定义?
#pragma曾经不适用于这个特定的编译器,那么解决方案是什么?有人把这个答案发给了一个类似的问题.它似乎对我不起作用.这个解决方案如何运作?
请不要与标题混淆,因为它已被某人询问,但不同的背景
Visual C++编译器(VS2008)中的以下代码未编译,而是抛出此异常:
std::ifstream input (fileName);
while (input) {
string s;
input >> s;
std::cout << s << std::endl;
};
Run Code Online (Sandbox Code Playgroud)
但是这个代码在cygwin g ++中编译得很好.有什么想法吗?
档案啊
#ifndef A_H_
#define A_H_
class A {
public:
virtual ~A();
virtual void doWork();
};
#endif
Run Code Online (Sandbox Code Playgroud)
文件Child.h
#ifndef CHILD_H_
#define CHILD_H_
#include "A.h"
class Child: public A {
private:
int x,y;
public:
Child();
~Child();
void doWork();
};
#endif
Run Code Online (Sandbox Code Playgroud)
和Child.cpp
#include "Child.h"
Child::Child(){
x = 5;
}
Child::~Child(){...}
void Child::doWork(){...};
Run Code Online (Sandbox Code Playgroud)
编译器说对vtable有一个未定义的引用A.我尝试了很多不同的东西,但没有一个有效.
我的目标是将类A作为接口,并从头部分离实现代码.
c++ ×7
c ×3
visual-c++ ×2
c++-faq ×1
coding-style ×1
control-flow ×1
cygwin ×1
declaration ×1
extern ×1
inheritance ×1
inline ×1
linker ×1
loops ×1
terminology ×1
vtable ×1