小编The*_* do的帖子

我必须从头开始吗?

如果我有:

std::size_t bagCapacity_ = 10;
std::size_t bagSize = 0;
A** bag = new A*[bagCapacity_];

while (capacity--)
{
  bag[capacity] = new A(bagSize++); //**here I'm loading this array from the end is it ok?**
}
Run Code Online (Sandbox Code Playgroud)

还可以从数组末尾开始删除那些对象吗?

while (capacity--)
{
  delete bag[capacity];
}
Run Code Online (Sandbox Code Playgroud)

代码中的问题.

c++ arrays

1
推荐指数
1
解决办法
172
查看次数

迭代器是否失效?

迭代器在以下情况下失效:

string b "Some string";
auto beg_ = b.begin();
auto end_ = b.end();
b.erase(beg_);
Run Code Online (Sandbox Code Playgroud)

c++ iterator

1
推荐指数
1
解决办法
178
查看次数

什么决定了什么时候在C++中为一个临时对象调用析构函数?

伙计我几天前问了一个问题,没有时间检查它并考虑它,但现在我尝试了其中一个解决方案,我不明白为什么它有效?我的意思是为什么析构函数会在行尾调用,如下所示:

#include "stdafx.h"
#include "coutn.h"
#define  coutn coutn()
int _tmain(int argc, _TCHAR* argv[])
{
    coutn << "Line one " << 1;//WHY DTOR IS CALLED HERE
    coutn << "Line two " << " and some text.";
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我认为它与对象的生命周期有关但我不知道是什么以及如何做.我想到它有两个未命名的对象,但它们不会超出范围,所以我无法理解dtor所谓的原因.
谢谢.

c++ reference

1
推荐指数
2
解决办法
459
查看次数

预订Windows 7中的编程

任何人都可以在C++中推荐关于Windows 7编程的好书吗?

c++ windows-7

1
推荐指数
1
解决办法
2253
查看次数

模糊运算符<<

#include "stdafx.h"
#include "Record.h"

template<class T>//If I make instead of template regular fnc this compiles  
//otherwise I'm getting an error (listed on the very bottom) saying  
// that operator << is ambiguous, WHY?
ostream& operator<<(ostream& out, const T& obj)
{
    out << "Price: " 
        << (obj.getPrice()) << '\t'//this line causes this error
        << "Count: "
        << obj.getCount()
        << '\n';
    return out;
}

int _tmain(int argc, _TCHAR* argv[])
{
    vector<Record> v;
    v.reserve(10);
    for (int i = 0; i < 10; ++i) …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

1
推荐指数
1
解决办法
1939
查看次数

移动ctor什么时候被调用?

鉴于课程:

class C
{
public:
    C()
    {
        cout << "Dflt ctor.";
    }
    C(C& obj)
    {
        cout << "Copy ctor.";
    }
    C(C&& obj)
    {
        cout << "Move ctor.";
    }
    C& operator=(C& obj)
    {
        cout << "operator=";
        return obj;
    }
    C& operator=(C&& obj)
    {
        cout << "Move operator=";
        return obj;
    }
};
Run Code Online (Sandbox Code Playgroud)

然后在主要:

int main(int argc, char* argv[])
{
    C c;
    C d = c;
    C e;
    e = c;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

正如您将从输出中看到的那样,复制ctor的"常规"版本operator=被调用但不是那些带有rvalue args的版本.所以我想问一下在什么情况下移动ctor并被operator=(C&&)调用?

c++ constructor c++11

1
推荐指数
1
解决办法
166
查看次数

继承问题

这是来自"Exceptional C++"的第24项,解决方案,页面底部的第一个子弹:

永远不要使用公共继承来实现"IS-ALMOST-A".我已经看到一些程序员,甚至是经验丰富的程序员,从基础公开继承并以保留基类语义的方式实现"大多数"被覆盖的虚函数.换句话说,在某些情况下,使用Derived对象作为Base将不会像合理的Base客户端所期望的那样运行.罗伯特·马丁经常引用的一个例子是从Rectangle类继承Square类通常被误导的想法"因为正方形是一个矩形".这在数学中可能是正确的,但在课堂上并不一定如此.例如,假设Rectangle类具有虚拟SetWidth(int)函数.然后广场' 设置宽度的实现也会自然地设置高度,使对象保持方形.然而,在系统的其他地方可能存在与Rectangle对象一起使用多态的代码,并且不会期望更改宽度也会改变高度.毕竟,对于一般的矩形来说,情况并非如此!这是违反LSP的公共继承的一个很好的例子,因为派生类不提供与基类相同的语义.它违反了公共继承的关键原则:"不再需要,也不能承诺." 这是违反LSP的公共继承的一个很好的例子,因为派生类不提供与基类相同的语义.它违反了公共继承的关键原则:"不再需要,也不能承诺." 这是违反LSP的公共继承的一个很好的例子,因为派生类不提供与基类相同的语义.它违反了公共继承的关键原则:"不再需要,也不能承诺."

我试过检查它,我写道:

// Square.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;
class Rectangle
{
private:
 unsigned width_;
 unsigned height_;
public:
 Rectangle(const unsigned width, const unsigned height):width_(width),height_(height)
 {/*Empty body*/ }
 unsigned GetWidth()const
 {
  return width_;
 }
 unsigned GetHeight()const
 {
  return height_;
 }
 virtual void SetWidth(const unsigned width)
 {
  width_ = width;
 }
 void SetHeight(const unsigned height)
 {
  height_ = height;
 }
 virtual ~Rectangle()
 {
  cout << …
Run Code Online (Sandbox Code Playgroud)

c++ inheritance

1
推荐指数
1
解决办法
285
查看次数

可以取消引用指针抛出?

我正在教自己使用异常安全模式编程的技术;)我想知道解除引用指针是否会引发异常?我认为所有C++程序员都知道所有保证不会抛出的操作会很有帮助,所以如果有人能编写这样的列表,我将非常感激.

c++ exception

1
推荐指数
1
解决办法
558
查看次数

返回一种对象;

是否可以返回对象的类型?例如,我想有这样的结构:

//pseudocode
    template<class T>
    void f(int value)
    {
    //depends on type T different action can be taken
    }

template<class T>
type getType(T obj)
{
return (type of obj);
}
Run Code Online (Sandbox Code Playgroud)

然后在主要:

f<getType(Object)>(value);  
Run Code Online (Sandbox Code Playgroud)

c++ templates

1
推荐指数
1
解决办法
194
查看次数

出乎意料的事情发生了

我正在尝试处理意外的异常,但无法使其工作.这是来自C++ Reference的示例

// set_unexpected example
#include <iostream>
#include <exception>
using namespace std;

void myunexpected () {
  cerr << "unexpected called\n";
  throw 0;     // throws int (in exception-specification)
}

void myfunction () throw (int) {
  throw 'x';   // throws char (not in exception-specification)
}

int main (void) {
  set_unexpected (myunexpected);
  try {
    myfunction();
  }
  catch (int) { cerr << "caught int\n"; }
  catch (...) { cerr << "caught other exception (non-compliant compiler?)\n"; }
  return 0;
} 
Run Code Online (Sandbox Code Playgroud)

他们说输出应该是:输出:

出乎意料的叫

抓住了 …

c++ exception

1
推荐指数
1
解决办法
202
查看次数