假设您有以下代码:
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> First{"example", "second" , "C++" , "Hello world" };
std::vector<std::string> Second{"Hello"};
First.swap(Second);
for(auto a : Second) std::cout << a << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
想象一下向量不是std::string,但类:
std::vector<Widget> WidgetVector;
std::vector<Widget2> Widget2Vector;
Run Code Online (Sandbox Code Playgroud)
用该std::vector::swap方法交换两个向量是否仍然安全:WidgetVector.swap(Widget2Vector);或者它会导致 UB?
#include<iostream>
using namespace std;
class base
{
public:
virtual void add() {
cout << "hi";
}
};
class derived : public base
{
private:
void add() {
cout << "bye";
}
};
int main()
{
base *ptr;
ptr = new derived;
ptr->add();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是 bye
我对这是如何实现没有问题.我理解你使用vtable和派生的vtable包含新的add()函数的地址.但是当我尝试在类外部访问它时,add()是私有的,不应该编译器生成错误吗?不知怎的,这似乎不对.
我开始Zed Shaw的学习C艰难的方式.我已经下载了XCode和命令行工具.但是当我编译第一个程序时:
int main(int argc, char *argv[]) {
puts("Hello world.");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到这个警告:
ex1.c:2:1:警告:C99中函数'puts'的隐式声明无效[-Wimplicit-function-declaration]
该程序可以正确编译和执行.
我正在使用OSX 10.8.3.输入'gcc -v'给出:
使用内置规格.目标:i686-apple-darwin11配置:/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/src/configure --disable-checking --enable-werror --prefix =/Applications/Xcode.app/Contents /Developer/usr/llvm-gcc-4.2 --mandir =/share/man --enable-languages = c,objc,c ++,obj-c ++ --program-prefix = llvm- --program-transform-name =/^ [cg] [^ .-]*$/s/$/ - 4.2/--with-slibdir =/usr/lib --build = i686-apple-darwin11 --enable-llvm =/private/var/tmp /llvmgcc42/llvmgcc42-2336.11~182/dst-llvmCore/Developer/usr/local --program-prefix = i686-apple-darwin11- --host = x86_64-apple-darwin11 --target = i686-apple-darwin11 - with-gxx-include-dir =/usr/include/c ++/4.2.1线程模型:posix gcc版本4.2.1(基于Apple Inc. build 5658)(LLVM build 2336.11.00)
请帮忙.
该标准是否指定了官方C++语法?
我搜索过,但没找到任何地方.
另外,我希望详细阅读一些关于C++语法的内容,比如它所属的语法类别等等.任何指向正确方向的链接都会有所帮助.
按类别,我的意思是
取自这里.
c++ standards grammar context-free-grammar chomsky-hierarchy
从历史上看,我喜欢打断表达式,以便在续行中显示"它明显不完整"的偏见:
var something = foo + bar
+ baz(mumble);
Run Code Online (Sandbox Code Playgroud)
这种态度来自于使用分号来终止表达式的语言.由于没有分号,第一行显然已经不完整了,所以最好让读者清楚第二行不完整.
替代方案是:
var something = foo + bar +
baz(mumble);
Run Code Online (Sandbox Code Playgroud)
这对我来说不是那么好.现在告诉它baz(mumble);不是独立的唯一方法(缩进除了)是扫描你的眼睛到前一行的末尾. (这可能是长的,因为你需要首先打破它.)
但是在奇怪的JavaScript领域,我开始看到人们将代码从看起来像第一种形式的东西改为第二种形式,并警告"自动分号插入".它肯定会引起一些令人惊讶的行为.当我把"学习自动分号插入是什么以及我是否应该这样做"放入我的无限任务队列时,并不是真的想深入研究那个切线.
当我确实调查它时,我发现自己半确定......但不确定......我对它的使用方式没有危险.似乎问题来自于我是否已经停止了+事故并写道:
var something = foo + bar
baz(mumble);
Run Code Online (Sandbox Code Playgroud)
...然后JavaScript会foo + bar为您插入分号,因为这两行都是完整的表达式.我推断,或许那些其他JavaScript程序员认为偏向"明显有意不完整"的位到最后一行是更好的,因为它指出了分号不在的位置.
然而,如果我已经正确地阐述了我的前提,那么我正在以一种随后的线条"明显不完整"的方式塑造我的虚线.如果情况并非如此,那么我首先不会认为我的方式是优势.
我是否正确,我的方式不是我描述如何使用线延续的条件的风险?是否有任何"看似不完整"的表达陷阱实际上以令人惊讶的方式完成?
为了提供一个"以令人惊讶的方式完成"的例子,考虑是否+ 1;可以将其解释为在一条线上只是正面的.它似乎可以:

但是jsfiddle给出了6个:
var x = 3 + 2
+ 1;
alert(x)
Run Code Online (Sandbox Code Playgroud)
也许这只是控制台中的一个怪癖,但它让我担心我的"只要第二行不是完全表达的解释就可以了".
可以理解的是,将缓冲区错误输出(或创建溢出),但如果12字节缓冲区中使用的字节少于12个,会发生什么?是否有可能或者空尾随时都是0?正交问题可能会有所帮助:缓冲区在实例化但未被应用程序使用时包含哪些内容?
我在Visual Studio中查看了几个宠物程序,似乎它们附加了0(或空字符),但我不确定这是否是一个可能因语言/编译器而异的MS实现.
抱歉,如果这个问题听起来很愚蠢,我就是在关注SO的专家并亲自尝试一些例子,这就是其中之一.我确实尝试了搜索选项,但没有找到这种答案.
class A
{
public:
A(){cout<<"A Contruction"<<endl;}
~A(){cout<<"A destruction"<<endl;}
};
int main()
{
vector<A> t;
t.push_back(A()); // After this line, when the scope of the object is lost.
}
Run Code Online (Sandbox Code Playgroud)
为什么类的析构函数被调用两次?
我对以下关于继承的小程序感到困惑:
#include<iostream>
using namespace std;
struct B {
virtual int f() { return 1; }
}; // f is public in B
class D : public B {
int f() { return 2; }
}; // f is private in D
int main()
{
D d;
B& b = d;
cout<<b.f()<<endl; // OK: B::f() is public, D::f() is invoked even though it's private
cout<<d.f()<<endl; // error: D::f() is private
}
Run Code Online (Sandbox Code Playgroud)
D::f()是私有的,D是公共继承从B,所以公共职能f …c++ ×7
standards ×3
c ×2
inheritance ×2
buffer ×1
c++-faq ×1
c++11 ×1
destructor ×1
ecmascript-5 ×1
gcc ×1
grammar ×1
javascript ×1
macos ×1
newline ×1
polymorphism ×1
private ×1
stdvector ×1
swap ×1
vector ×1