下面的代码被编译在 g++ 4.6.3 for Linux
#include <iostream>
class A {
  public:  
    int x;
    std::string c;
    A(int x,std::string c):x(10),c("Hi"){
    }
    ~A(){
      std::cout << "Deleting A()" << std::endl;
    }
};
class B : public A {
  public:
    B():A(20,"Hello"){
    }
    ~B(){
      std::cout << "Deleting B()" << std::endl;
    }
};
int main(){
  B o;
  std::cout << o.x << std::endl;
  std::cout << o.c << std::endl;
  return(0);
}
但它没有做应该做的事情,类型 B 无法更改它从 A 继承的那 2 个变量的值。
关于为什么这不能正常工作的任何解释?
我希望有一个简单的问题,我无法找到有关初始化“变量”的具体答案。这是有问题的初始化列表:Triangle.cc
Triangle::Triangle(Vector _a, Vector _b, Vector s, Point o)
    : a(_a), b(_b), Shape(o, new RectilinearAnimationStrategy(s, bBoxMaxX(), bBoxMinX(), bBoxMaxY(), bBoxMinY())){}
我知道你必须按照类中定义的顺序初始化变量,但是当你初始化一个对象时,这种情况又如何呢?我需要a并b在调用之前显式初始化Shape,以便将正确的值返回给RectilinearAnimationStrategy. 我尝试的一切都给了我
Triangle.h:在构造函数中
Triangle::Triangle(Vector, Vector, Vector, Point):
Triangle.h:11:警告:Triangle::b将在
Triangle.cc:9之后初始化:警告:基础Shape
Triangle.cc:9:警告:在这里初始化时
非常感谢任何帮助,谢谢。
当前设置和给出的第一个答案都没有得到该错误,但输出没有考虑a和b考虑(在调用 Shape 构造函数时未初始化)
我想用这样的初始化列表初始化一个结构:
struct S
{
    int a;
    int b;
    // S() : a(0), b(0){}  // uncommenting will cause compile error: 
                           // error C2440: 'initializing' : cannot convert from 'initializer-list' to 'S'
    // S(int aArg, int bArg) : a(aArg), b(bArg) {}    // adding this removes the error
}
int main()
{
    S s{1,2};   // initialise with list
}
有一个很好的理由,为什么显式声明的默认构造函数会导致错误?我认为初始化列表的位置是为了避免程序员编写像第二个构造函数这样繁琐的代码.
这个问题表明std::initializer_list<int>::const_iterator类型只是一个普通的int const*指针,但是这个答案(对于同一个问题)引用的标准的措辞听起来更像是一个建议,而不是对我的保证.
在我的代码中,问题发生如下:我有一个处理a的子范围的函数initializer_list,在不同的情况下,我重用传递单个元素的便捷函数的代码(作为单元素范围):
void doStuff(int const* begin, int const* end)
{ ... do stuff ... }
  // main use; in the real code, 'init' is a parameter of a function
  std::initializer_list<int> init({1, 2, 3, 4, 5});
  doStuff(init.begin() + 1, init.end());
  // alternative use of doStuff, relies on the pointer assumption
  int x = 6;
  doStuff(&x, (&x) + 1);
}
这种结构依赖于迭代器确实是指针的事实.它至少适用于我的clang ++ 3.9编译器.我可以依靠它来始终工作,即指针假设是否可移植?保证便携?或者我应该将参数类型更改doStuff为模板参数,以确保安全吗?
template <typename Iterator>
void doStuff(Iterator begin, Iterator …我知道,该向量类以下列方式初始化:
Vector::Vector(initializer_list<double> lst)
    :size{static_cast<int>(lst.size())},elem{new double[static_cast<int>(lst.size())]}
    {
        copy(lst.begin(),lst.end(),elem);
    }
这是我的矩阵类:
class Matrix{
private:
    int row;
    int col;
    double elem**
public:
    //Default Constructor:
    Matrix(int row,int col);
    //Initialized list constructor:
    Matrix(initializer_list<initializer_list<double>> lst);
我想,我应该在初始化列表中使用初始化的 lis 来创建初始化矩阵。从语法的角度我该怎么做?
我试图std::bind去一个lambda时遇到了错误.以下代码示例编译正常:
#include <functional>
#include <iostream>
int main()
{
    const auto lambda1 = [](int a, int b){ return a + b; };
    std::cout << (std::bind( lambda1, std::placeholders::_1, 2))(2)
              << std::endl;
    return 0;
}
但以下不是:
#include <functional>
#include <iostream>
int main()
{
    const auto lambda2 { [](int a, int b){ return a + b; } }; // Note the "{ }-initialization"
    std::cout << (std::bind( lambda2, std::placeholders::_1, 2))(2)
              << std::endl;
    return 0;
}
这是我使用Visual Studio 2013得到的错误输出,更新4:
1>------ Build started: Project: Project1, …我想在所有者构造函数中调用成员对象的构造函数,但由于依赖性,无法在初始化列表中构造成员对象.初始化后如何调用构造函数?我真的不想使用init方法
考虑以下代码:
#include <initializer_list>
class C {
public:
    C() = delete;
    C(int) {}
};
class D {
public:
    D(std::initializer_list<C> il) {} 
};
int main()
{
    std::initializer_list<C> il{};  // fine: empty list, no need to construct C
    D d2(il);                       // fine: calls initializer_list ctor with empty list
    D d3{il};                       // ditto
    D d4({});                       // still fine
    D d5{{}};                       // error: use of deleted function 'C::C()' 
                                    // WHY is the constructor of 'C' required here?
}
我想D d5{{}};会用一个空列表调用initializer_list构造函数D …
我有一个vec<T>常量长度等于 3 的数学向量模板类。它看起来像这样:
template <typename T>
class vec{
public:
    vec() { /**/ }
    vec(std::initializer_list<T> list) { /**/ }
private:
    std::array<T, 3> data;
};
我知道,可以std::array用std::initializer_list这样的方式初始化:
std::array<int, 3> a = {1, 2, 3};
所以我想有一个构造函数std::initializer_list来初始化我的向量,如下所示:
vec<int> v = {1, 2, 3};
我也有一个解决方案:只需遍历std::initializer_list元素并写入data:
vec(std::initializer_list<T> list) {
    size_t i = 0;
    for (auto it = list.begin(); it != list.end(); it++) {
        data[i++] = *it;
    }
}
我试图让构造函数看起来像这样(因为std::array有一个带有 的构造函数std::initializer_list):
vec(std::initializer_list<T> …请解释此函数如何返回两个值。它采用一个数组并从数组中返回总和等于目标总和的两个数字。
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
    int a = 0;
    int b = 0;
    for (int i=0; i<nums.size()-1; i++)
    {
        for (int j=i+1; j<nums.size(); j++)
        {
            if (nums[i] + nums[j] == target)
            {
                a = i;
                b = j;
            }
        }
    }
    return {a, b};
    }
};
c++ ×10
initializer-list ×10
c++11 ×4
constructor ×2
arrays ×1
c++14 ×1
class ×1
inheritance ×1
iterator ×1
lambda ×1
matrix ×1
oop ×1
pointers ×1
return ×1
stdbind ×1